搜索
首页后端开发php教程为自定义帖子类型创建多功能插件

通常,您需要做出选择并决定如何实现特定功能。 WordpPress 对几乎任何问题都有相当灵活的方法,在本教程中,我们将了解自定义帖子类型 API 的灵活性。

您可以使用自定义帖子类型执行许多操作,包括在某些自定义页面、幻灯片、图库、甚至组合项目上生成和管理某些帖子。有些人还不知道它们的存在,其他人则不完全了解他们的选择以及如何使用它们。我们将在以下步骤中尝试构建一个最好使用自定义帖子类型的特定插件。更准确地说,我们将构建一个插件,用于创建一种自定义帖子类型,动态创建和管理其他自定义帖子类型(就像这里的 Inception!)。


第 1 步计划

在开始之前,我们将详细了解我们将在本教程中构建的内容、方式和原因。本教程的目的是帮助您熟悉 WordPress 中的自定义帖子类型。

为此,我们将构建一个插件,用于创建一个主要的自定义帖子类型,然后使用该自定义帖子类型中的帖子以及设置元框,根据元框创建其他动态自定义帖子类型界面选项。这将帮助您详细了解所有自定义帖子选项以及它可以提供的一些可能性。出于本教程的目的,暗示您有一个运行 PHP 和 MySQL 的工作 Apache 服务器,并且安装了 WordPress,以便您可以在 3.0+ 版本的 WordPress 上测试插件代码。

每个帖子都将被解释为新的自定义帖子类型,我们将使用元框将每个帖子的配置选项保存在自定义字段中,其中包含大量表单、复选框、下拉框和文本框,这些表单、复选框、下拉框和文本框将存储并显示数据,使用户可以轻松修改任何内容。

由于该插件有 450 行代码,教程中并非每一行代码都会存在,因为大多数代码都是重复的,只是变量不同。但该插件的所有功能都将得到解释,并以源代码为例。


第 2 步创建并理解 Hooks

如果我们想要获得自定义帖子类型功能、元框功能,并且想要在更新或发布操作时保存数据,我们需要钩子,并且需要专门实现它们以获得所需的效果。

add_action('save_post', 'cpt_save_postdata');
add_action('add_meta_boxes', 'cpt_add_meta_boxes');
add_action('init', 'init_custom_post_types');

在本教程中我们只需要三个钩子,没有过滤器,核心功能中没有其他任何东西。首先,我们需要一个钩子在其回调函数中生成自定义帖子类型。为此,我们将使用 init 标记挂钩和名为 'init_custom_post_types' 的回调函数。这就是所有魔法发生的地方,即我们声明自定义帖子类型的地方,以及通过从数据库中提取值并在循环中生成动态自定义帖子类型来使它们动态化的地方。这部分代码稍后讨论。

我们需要的另一个钩子是 'add_meta_boxes' 标记钩子,它指向回调函数 'cpt_add_meta_boxes' ,用于在生成所有其他自定义的主自定义帖子类型内生成元框帖子类型。这是我们创建表单的地方,该表单将存储和更新我们需要操作以创建功能的数据。

我们使用的最后一个钩子是 'save_post' ,其回调函数为 'cpt_save_postdata' ,目的是保存元框发布的数据。当用户在管理面板中提交主要自定义帖子类型的页面时,所有表单字段都会被提交,这是我们用来获取该内容并将其保存/更新到数据库中以供以后使用的钩子。


第 3 步创建主要自定义帖子类型

首先,我们需要创建一个通用的自定义帖子类型来处理所有动态内容。为此,首先我们使用硬编码数据创建一个通用的自定义帖子类型,如下例所示。首先我们创建标签。 labels 参数是解释为数组的主要参数值之一。数组值由其标识符给出,具体来说 name 表示自定义帖子类型的主要通用名称,通常是自定义帖子类型名称的复数字符串, singular_name 表示单数形式的自定义帖子类型对象名称, add_new 添加新字符串文本,add_new_item 默认添加新帖子/页面文本,edit_item 作为编辑帖子或页面的默认文本,all_items 用于显示所有自定义帖子类型名称文本根据要求(例如:所有汽车), view_itemssearch_items 的使用方式与前面的示例类似,只是出于明显不同的目的, not_foundnot_fount_in_trashparent_item_colon 也是替换用于某些页面的文本您可能想要为您创建的任何自定义帖子类型更改的帖子(自定义帖子类型)功能。

在下一个示例中,$labels 数组使用 CPT 替换默认的 post/posts/page/pages 文本。我想这对于某些读者来说是不必要的,但为了确保我不会错过这个想法,CPT 代表自定义帖子类型,并且在本教程中将以多种方式使用。

需要注意的是,此代码被插入到 init_custom_post_types 回调函数中才能正常工作。

$labels = array(
	'name' => _x('CPT', 'post type general name'),
	'singular_name' => _x('CPT', 'post type singular name'),
	'add_new' => _x('Add New CPT', 'CPT'),
	'add_new_item' => __('Add New Post type'),
	'edit_item' => __('Edit CPT'),
	'new_item' => __('New CPT'),
	'all_items' => __('All CPT'),
	'view_item' => __('View CPT'),
	'search_items' => __('Search CPT'),
	'not_found' => __('No CPT found'),
	'not_found_in_trash' => __('No CPT found in Trash'),
	'parent_item_colon' => '',
	'menu_name' => __('CPT')
);

$args = array(
	'labels' => $labels,
	'public' => true,
	'publicly_queryable' => true,
	'show_ui' => true,
	'show_in_menu' => true,
	'query_var' => true,
	'rewrite' => true,
	'capability_type' => 'post',
	'has_archive' => true,
	'hierarchical' => false,
	'menu_position' => null,
	'supports' => array('title')
);
register_post_type('CPT', $args);

生成自定义帖子类型所需的主数组是 $args 数组,其唯一目的是设置自定义帖子类型的主要选项并管理其功能。

  • 第一个数组变量名为 labels,我们之前已经处理过该值的结构。
  • public – 一个布尔变量,true 或 false,表示自定义帖子类型的可用性(如果它对于管理界面或主题中的前端用户是公开的)。
  • publicly_queryable – 又是一个布尔值,用于设置前端是否能够查询自定义帖子类型的结果。
  • show_ui – 一个布尔值,指示是否为自定义帖子类型生成默认管理内容。
  • show_in_menu – 另一个布尔变量,需要 show_ui 才能正常工作,因为其目的是在 WordPress 管理面板的菜单中显示或不显示帖子类型。
  • query_var – 为此自定义帖子类型创建查询 var 键。您可以将其用作布尔值或字符串,如果为 true 则为默认值,如果为 false 则禁用查询 var 键的使用,而 string 设置自定义键。在我们的示例中,我们使用默认值并将其设置为 true。
  • rewrite – 这可以防止重写此帖子类型。
  • capability_type – 最后,一个字符串,用于设置自定义帖子类型是帖子还是页面,采用纯文本字符串。我们正在使用邮寄。
  • has_archive – 此参数启用帖子存档并默认使用 post_type 作为存档 slug。
  • hierarchical – 一个布尔值,用于设置帖子类型是否为分层结构,即是否允许指定父级。
  • menu_position – 表示帖子类型在菜单中显示的位置的字符串。这些值大约从 5 到 100。
  • supports – 该值再次具有数组值,这次包含标题、编辑器、缩略图、自定义字段、引用、修订、页面属性和帖子格式等值。

主 args 数组用于下一个 register_post_type 函数,该函数的第一个参数是自定义帖子类型名称(在我们的例子中是 CPT),第二个参数是 $args 变量。

这几乎涵盖了创建我们的主要自定义帖子类型,上面给出并解释了示例。


第 4 步编码元框

首先,我们介绍了钩子,其中一个钩子实现了一个标签,该标签具有专门为元框实现而设计的回调函数。该函数如下所示:

function cpt_add_meta_boxes() {
	add_meta_box('cpt_meta_id', 'Custom Post Type Settings', 'cpt_inner_custom_box', 'CPT', 'normal');
}

在函数内部,我们有 add_meta_box 函数,它具有用于实现元框的默认参数,即唯一的元框 id、它的标题、回调函数和应应用它的自定义帖子类型,也是最后但并非最不重要的一点,位置(我们将其设置为正常,因为我们希望它位于编辑器下方)。

这就是使用 API 生成的内容,但是我们要填充什么呢?好吧,我们需要用之前讨论的第 3 步中的所有选项来填充它,为此,我们将创建表单字段并根据其类型填充数据。

有些是复选框

我们对需要布尔值的数组参数使用复选框。选中为 true,未选中为 false。

	<td><input type="checkbox" <?php
		if ($cp_publicly_queryable == "on") {
			echo "checked";
		}
	?> name="cp_publicly_queryable" /> Publicly Queryable </td>

其他将是下拉框

某些数组参数需要从多个元素中进行选择。为此,我们使用带有硬编码值的下拉框来满足我们的需求。

<td>Menu Position:<br/>
	<select name="cp_menu_position">
		<option value="post" <?php
		if ($cp_menu_position == "post") {
			echo "selected";
		}
		?>>Post</option>
		<option value="page" <?php
		if ($cp_menu_position == "page") {
			echo "selected";
		}
		?>>Page</option>
	</select>
</td>

和其他文本字段

我们将在这些表单字段中存储字符串值。

<td>General name:<br/> <input type="text" name="cp_general_name" value="<?php echo $cp_general_name; ?>"/></td>

您可能已经注意到它们充满了 PHP 变量并使用数据运行。但您可能会问自己,这些变量从何而来,数据又如何呢?要回答这个问题,我们必须进入下一步并保存刚才创建的表单字段。但是因为我不想让您来回执行这些步骤,所以我会要求您假设所有数据都以某种方式保存在数据库中,我将向您展示如何操作,我们需要做的就是提取它到变量中,如下例所示:

global $post;

$cp_public = get_post_meta($post->ID, 'cp_public', true);

在我们的元框回调函数中,我们声明全局变量 $post 以稍后获取当前帖子 id,并且由于数据存储为自定义帖子,因此我们将使用 get_post_meta 函数来获取元为每个变量存储的键值,如上例所示,其中第一个参数是帖子 ID,第二个参数是元键,第三个参数是一个布尔值,告诉函数返回字符串而不是数组。

本教程中使用了 27 个变量,它们存储为自定义帖子,如下所示:

	$cp_public = get_post_meta($post->ID, 'cp_public', true);
	$cp_publicly_queryable = get_post_meta($post->ID, 'cp_publicly_queryable', true);
	$cp_show_ui = get_post_meta($post->ID, 'cp_show_ui', true);
	$cp_show_in_menu = get_post_meta($post->ID, 'cp_show_in_menu', true);
	$cp_query_var = get_post_meta($post->ID, 'cp_query_var', true);
	$cp_rewrite = get_post_meta($post->ID, 'cp_rewrite', true);
	$cp_has_archive = get_post_meta($post->ID, 'cp_has_archive', true);
	$cp_hierarchical = get_post_meta($post->ID, 'cp_hierarchical', true);
	$cp_capability_type = get_post_meta($post->ID, 'cp_capability_type', true);
	$cp_menu_position = get_post_meta($post->ID, 'cp_menu_position', true);
	$cp_s_title = get_post_meta($post->ID, 'cp_s_title', true);
	$cp_s_editor = get_post_meta($post->ID, 'cp_s_editor', true);
	$cp_s_author = get_post_meta($post->ID, 'cp_s_author', true);
	$cp_s_thumbnail = get_post_meta($post->ID, 'cp_s_thumbnail', true);
	$cp_s_excerpt = get_post_meta($post->ID, 'cp_s_excerpt', true);
	$cp_s_comments = get_post_meta($post->ID, 'cp_s_comments', true);
	$cp_general_name = get_post_meta($post->ID, 'cp_general_name', true);
	$cp_singular_name = get_post_meta($post->ID, 'cp_singular_name', true);
	$cp_add_new = get_post_meta($post->ID, 'cp_add_new', true);
	$cp_add_new_item = get_post_meta($post->ID, 'cp_add_new_item', true);
	$cp_edit_item = get_post_meta($post->ID, 'cp_edit_item', true);
	$cp_new_item = get_post_meta($post->ID, 'cp_new_item', true);
	$cp_all_items = get_post_meta($post->ID, 'cp_all_items', true);
	$cp_view_item = get_post_meta($post->ID, 'cp_view_item', true);
	$cp_search_items = get_post_meta($post->ID, 'cp_search_items', true);
	$cp_not_found = get_post_meta($post->ID, 'cp_not_found', true);
	$cp_not_found_in_trash = get_post_meta($post->ID, 'cp_not_found_in_trash', true);
	$cp_parent_item_colon = get_post_meta($post->ID, 'cp_parent_item_colon', true);

您需要了解的是,它们都是以相同的方式提取的,除了它们的值之外,在这个长长的变量列表中没有任何特殊或不同的地方。当然,这个想法是,此代码从数据库获取数据,并稍后在每个表单字段中使用,以将管理面板元框中的数据显示为选定值、字符串或布尔值的选中复选框价值观。在附加的可下载文件中可以更好地看到所有功能。

到目前为止,这就是基于我们的实现的自定义帖子类型元框内容的自定义帖子的样子。

为自定义帖子类型创建多功能插件为自定义帖子类型创建多功能插件为自定义帖子类型创建多功能插件

第5步保存数据

从元框提交的所有表单数据都需要保存到数据库中,以便稍后可以使用它来使自定义帖子类型动态化以及元框的功能中。为了保存数据,我们使用 cpt_save_postdata 函数作为我们为此创建的回调函数。但这个函数需要填充实际保存数据的功能。那么,我们如何保存所有这些元素的数据。最简单、最明显的答案是自定义字段。我们将把所有这些值存储在主要自定义帖子类型单个帖子的自定义字段中,因为当我们根据某个查询的帖子提取它们时,以后管理起来会容易得多。

这里是在之前指定的函数中运行所需的所有代码,以便保存我们需要的所有数据。

global $post;
if ($_POST['cpt-hidd'] == 'true') {
	$cp_public = get_post_meta($post->ID, 'cp_public', true);
	$cp_publicly_queryable = get_post_meta($post->ID, 'cp_publicly_queryable', true);
	$cp_show_ui = get_post_meta($post->ID, 'cp_show_ui', true);
	$cp_show_in_menu = get_post_meta($post->ID, 'cp_show_in_menu', true);
	$cp_query_var = get_post_meta($post->ID, 'cp_query_var', true);
	$cp_rewrite = get_post_meta($post->ID, 'cp_rewrite', true);
	$cp_has_archive = get_post_meta($post->ID, 'cp_has_archive', true);
	$cp_hierarchical = get_post_meta($post->ID, 'cp_hierarchical', true);
	$cp_capability_type = get_post_meta($post->ID, 'cp_capability_type', true);
	$cp_menu_position = get_post_meta($post->ID, 'cp_menu_position', true);
	$cp_s_title = get_post_meta($post->ID, 'cp_s_title', true);
	$cp_s_editor = get_post_meta($post->ID, 'cp_s_editor', true);
	$cp_s_author = get_post_meta($post->ID, 'cp_s_author', true);
	$cp_s_thumbnail = get_post_meta($post->ID, 'cp_s_thumbnail', true);
	$cp_s_excerpt = get_post_meta($post->ID, 'cp_s_excerpt', true);
	$cp_s_comments = get_post_meta($post->ID, 'cp_s_comments', true);
	$cp_general_name = get_post_meta($post->ID, 'cp_general_name', true);
	$cp_singular_name = get_post_meta($post->ID, 'cp_singular_name', true);
	$cp_add_new = get_post_meta($post->ID, 'cp_add_new', true);
	$cp_add_new_item = get_post_meta($post->ID, 'cp_add_new_item', true);
	$cp_edit_item = get_post_meta($post->ID, 'cp_edit_item', true);
	$cp_new_item = get_post_meta($post->ID, 'cp_new_item', true);
	$cp_all_items = get_post_meta($post->ID, 'cp_all_items', true);
	$cp_view_item = get_post_meta($post->ID, 'cp_view_item', true);
	$cp_search_items = get_post_meta($post->ID, 'cp_search_items', true);
	$cp_not_found = get_post_meta($post->ID, 'cp_not_found', true);
	$cp_not_found_in_trash = get_post_meta($post->ID, 'cp_not_found_in_trash', true);
	$cp_parent_item_colon = get_post_meta($post->ID, 'cp_parent_item_colon', true);

	update_post_meta($post->ID, 'cp_public', $_POST['cp_public'], $cp_public);
	update_post_meta($post->ID, 'cp_publicly_queryable', $_POST['cp_publicly_queryable'], $cp_publicly_queryable);
	update_post_meta($post->ID, 'cp_show_ui', $_POST['cp_show_ui'], $cp_show_ui);
	update_post_meta($post->ID, 'cp_show_in_menu', $_POST['cp_show_in_menu'], $cp_show_in_menu);
	update_post_meta($post->ID, 'cp_query_var', $_POST['cp_query_var'], $cp_query_var);
	update_post_meta($post->ID, 'cp_rewrite', $_POST['cp_rewrite'], $cp_rewrite);
	update_post_meta($post->ID, 'cp_has_archive', $_POST['cp_has_archive'], $cp_has_archive);
	update_post_meta($post->ID, 'cp_hierarchical', $_POST['cp_hierarchical'], $cp_hierarchical);
	update_post_meta($post->ID, 'cp_capability_type', $_POST['cp_capability_type'], $cp_capability_type);
	update_post_meta($post->ID, 'cp_menu_position', $_POST['cp_menu_position'], $cp_menu_position);
	update_post_meta($post->ID, 'cp_s_title', $_POST['cp_s_title'], $cp_s_title);
	update_post_meta($post->ID, 'cp_s_editor', $_POST['cp_s_editor'], $cp_s_editor);
	update_post_meta($post->ID, 'cp_s_author', $_POST['cp_s_author'], $cp_s_author);
	update_post_meta($post->ID, 'cp_s_thumbnail', $_POST['cp_s_thumbnail'], $cp_s_thumbnail);
	update_post_meta($post->ID, 'cp_s_excerpt', $_POST['cp_s_excerpt'], $cp_s_excerpt);
	update_post_meta($post->ID, 'cp_s_comments', $_POST['cp_s_comments'], $cp_s_comments);
	update_post_meta($post->ID, 'cp_general_name', $_POST['cp_general_name'], $cp_general_name);
	update_post_meta($post->ID, 'cp_singular_name', $_POST['cp_singular_name'], $cp_singular_name);
	update_post_meta($post->ID, 'cp_add_new', $_POST['cp_add_new'], $cp_add_new);
	update_post_meta($post->ID, 'cp_add_new_item', $_POST['cp_add_new_item'], $cp_add_new_item);
	update_post_meta($post->ID, 'cp_edit_item', $_POST['cp_edit_item'], $cp_edit_item);
	update_post_meta($post->ID, 'cp_new_item', $_POST['cp_new_item'], $cp_new_item);
	update_post_meta($post->ID, 'cp_all_items', $_POST['cp_all_items'], $cp_all_items);
	update_post_meta($post->ID, 'cp_view_item', $_POST['cp_view_item'], $cp_view_item);
	update_post_meta($post->ID, 'cp_search_items', $_POST['cp_search_items'], $cp_search_items);
	update_post_meta($post->ID, 'cp_not_found', $_POST['cp_not_found'], $cp_not_found);
	update_post_meta($post->ID, 'cp_not_found_in_trash', $_POST['cp_not_found_in_trash'], $cp_not_found_in_trash);
	update_post_meta($post->ID, 'cp_parent_item_colon', $_POST['cp_parent_item_colon'], $cp_parent_item_colon);
}

首先,我们像之前在元框功能顶部所做的那样提取变量,然后使用 update_post_meta 函数更新它们,我们给出参数:帖子 id、元键名称、新值,旧值。瞧,我们更新了一个值的自定义字段。我们需要重复您在上面的示例中看到的过程,以涵盖我们需要的所有值和所有选项。

这就是保存数据的方法。


第 6 步创建动态自定义帖子类型

那么我们如何获取刚刚保存的所有数据并使其动态化?我们如何使用它来生成自定义帖子类型?很简单,我们查询主要的自定义帖子类型,对于每个循环帖子,我们提取自定义字段并将数据相应地放置在数组中。

让我们看看如何做到这一点。首先,我们将此代码放在 init_custom_post_types 函数内的主自定义帖子类型代码的正下方。因此,我们首先创建查询:

$the_query = new WP_Query(array('post_type' => array('CPT')));
while ($the_query->have_posts()) : $the_query->the_post();

我们创建一个名为 $the_query 的变量,在其中存储调用的主类函数 wp_query 的内容,以及一个数组的参数,该数组的值是 post_type 主要自定义帖子类型的名称,即CPT。然后我们开始循环。在循环内,我们使用与从数据库中提取变量作为自定义字段相同的方法来生成我们想要保存所需值的变量:

global $post;
//*************************get the values
$cp_public = get_post_meta($post->ID, 'cp_public', true);
if ($cp_public == "on") {
	$cp_public = true;
}
else {
	$cp_public = false;
}
$cp_publicly_queryable = get_post_meta($post->ID, 'cp_publicly_queryable', true);
if ($cp_publicly_queryable == "on") {
	$cp_publicly_queryable = true;
}
else {
	$cp_publicly_queryable = false;
}
$cp_show_ui = get_post_meta($post->ID, 'cp_show_ui', true);
if ($cp_show_ui == "on") {
	$cp_show_ui = true;
}
else {
	$cp_show_ui = false;
}
$cp_show_in_menu = get_post_meta($post->ID, 'cp_show_in_menu', true); //
if ($cp_show_in_menu == "on") {
	$cp_show_in_menu = true;
}
else {
	$cp_show_in_menu = false;
}
$cp_query_var = get_post_meta($post->ID, 'cp_query_var', true); //
if ($cp_query_var == "on") {
	$cp_query_var = true;
}
else {
	$cp_query_var = false;
}
$cp_rewrite = get_post_meta($post->ID, 'cp_rewrite', true); //
if ($cp_rewrite == "on") {
	$cp_rewrite = true;
}
else {
	$cp_rewrite = false;
}
$cp_has_archive = get_post_meta($post->ID, 'cp_has_archive', true); //
if ($cp_has_archive == "on") {
	$cp_has_archive = true;
}
else {
	$cp_has_archive = false;
}
$cp_hierarchical = get_post_meta($post->ID, 'cp_hierarchical', true);
if ($cp_hierarchical == "on") {
	$cp_hierarchical = true;
}
else {
	$cp_hierarchical = false;
}
$cp_capability_type = get_post_meta($post->ID, 'cp_capability_type', true);
$cp_menu_position = get_post_meta($post->ID, 'cp_menu_position', true);
$cp_s_title = get_post_meta($post->ID, 'cp_s_title', true);
if ($cp_s_title == "on") {
	$cp_s[] = 'title';
}
$cp_s_editor = get_post_meta($post->ID, 'cp_s_editor', true);
if ($cp_s_editor == "on") {
	$cp_s[] = 'editor';
}
$cp_s_author = get_post_meta($post->ID, 'cp_s_author', true);
if ($cp_s_author == "on") {
	$cp_s[] = 'author';
}
$cp_s_thumbnail = get_post_meta($post->ID, 'cp_s_thumbnail', true);
if ($cp_s_thumbnail == "on") {
	$cp_s[] = 'thumbnail';
}
$cp_s_excerpt = get_post_meta($post->ID, 'cp_s_excerpt', true);
if ($cp_s_excerpt == "on") {
	array_push($cp_s, 'excerpt');
}
$cp_s_comments = get_post_meta($post->ID, 'cp_s_comments', true);
if ($cp_s_comments == "on") {
	array_push($cp_s, 'comments');
}
$cp_general_name = get_post_meta($post->ID, 'cp_general_name', true);
$cp_singular_name = get_post_meta($post->ID, 'cp_singular_name', true);
$cp_add_new = get_post_meta($post->ID, 'cp_add_new', true);
$cp_add_new_item = get_post_meta($post->ID, 'cp_add_new_item', true);
$cp_edit_item = get_post_meta($post->ID, 'cp_edit_item', true);
$cp_new_item = get_post_meta($post->ID, 'cp_new_item', true);
$cp_all_items = get_post_meta($post->ID, 'cp_all_items', true);
$cp_view_item = get_post_meta($post->ID, 'cp_view_item', true);
$cp_search_items = get_post_meta($post->ID, 'cp_search_items', true);
$cp_not_found = get_post_meta($post->ID, 'cp_not_found', true);
$cp_not_found_in_trash = get_post_meta($post->ID, 'cp_not_found_in_trash', true);
$cp_parent_item_colon = get_post_meta($post->ID, 'cp_parent_item_colon', true);

因为我们现在处于查询循环内,所以我们可以使用全局 $post 变量来获取我们需要的 ID。此外,您可能已经注意到 if 在这里和那里为大多数变量设置了一些条件。这些存在是因为 true 布尔值或某些其他字符串值需要从它们表示的 "on" 字符串正确转换为它们需要的 true 布尔值。

完成所有这些操作后,我们将把变量插入到动态自定义帖子类型实现的数组中:

	$labels = array(
		'name' => _x(get_the_title($post->ID), 'post type general name'),
		'singular_name' => _x($cp_singular_name, 'post type singular name'),
		'add_new' => _x($cp_add_new, get_the_title($post->ID)),
		'add_new_item' => __($cp_add_new_item),
		'edit_item' => __($cp_edit_item),
		'new_item' => __($cp_new_item),
		'all_items' => __($cp_all_items),
		'view_item' => __($cp_view_item),
		'search_items' => __($cp_search_items),
		'not_found' => __($cp_not_found),
		'not_found_in_trash' => __($cp_not_found_in_trash),
		'parent_item_colon' => __($cp_parent_item_colon),
		'menu_name' => __(get_the_title($post->ID))
	);

	$args = array(
		'labels' => $labels,
		'public' => $cp_public,
		'publicly_queryable' => $cp_publicly_queryable,
		'show_ui' => $cp_show_ui,
		'show_in_menu' => $cp_show_in_menu,
		'query_var' => $cp_query_var,
		'rewrite' => $cp_rewrite,
		'capability_type' => 'post',
		'has_archive' => $cp_has_archive,
		'hierarchical' => $cp_hierarchical,
		'menu_position' => $cp_menu_position,
		'supports' => $cp_s
	);
	register_post_type(get_the_title($post->ID), $args);
endwhile;

帖子标题用作自定义帖子类型的主名称,元框设置用作其余属性。这就是在 WordPress 中实现动态自定义帖子类型所需的全部内容。您可能已经注意到,我们还在上一个代码的末尾关闭了循环。


结论

自定义帖子类型并不难处理,并且它们在您可能需要的任何功能中都非常灵活,甚至可以使用其他挂钩、过滤器或自定义函数。在本教程中,我们设法在单个插件中介绍自定义帖子类型管理的特定动态实现,并根据您的需求使用代码和文件进行了解释。当然,自定义帖子类型的使用并不限于这些示例,因为它可以用于多种类型的实现、插件和主题,以多种方式纠缠在一起,以不同的方式查询,以多种方式过滤或操作。 p>

以上是为自定义帖子类型创建多功能插件的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
PHP:服务器端脚本语言的简介PHP:服务器端脚本语言的简介Apr 16, 2025 am 12:18 AM

PHP是一种服务器端脚本语言,用于动态网页开发和服务器端应用程序。1.PHP是一种解释型语言,无需编译,适合快速开发。2.PHP代码嵌入HTML中,易于网页开发。3.PHP处理服务器端逻辑,生成HTML输出,支持用户交互和数据处理。4.PHP可与数据库交互,处理表单提交,执行服务器端任务。

PHP和网络:探索其长期影响PHP和网络:探索其长期影响Apr 16, 2025 am 12:17 AM

PHP在过去几十年中塑造了网络,并将继续在Web开发中扮演重要角色。1)PHP起源于1994年,因其易用性和与MySQL的无缝集成成为开发者首选。2)其核心功能包括生成动态内容和与数据库的集成,使得网站能够实时更新和个性化展示。3)PHP的广泛应用和生态系统推动了其长期影响,但也面临版本更新和安全性挑战。4)近年来的性能改进,如PHP7的发布,使其能与现代语言竞争。5)未来,PHP需应对容器化、微服务等新挑战,但其灵活性和活跃社区使其具备适应能力。

为什么要使用PHP?解释的优点和好处为什么要使用PHP?解释的优点和好处Apr 16, 2025 am 12:16 AM

PHP的核心优势包括易于学习、强大的web开发支持、丰富的库和框架、高性能和可扩展性、跨平台兼容性以及成本效益高。1)易于学习和使用,适合初学者;2)与web服务器集成好,支持多种数据库;3)拥有如Laravel等强大框架;4)通过优化可实现高性能;5)支持多种操作系统;6)开源,降低开发成本。

揭穿神话:PHP真的是一种死语吗?揭穿神话:PHP真的是一种死语吗?Apr 16, 2025 am 12:15 AM

PHP没有死。1)PHP社区积极解决性能和安全问题,PHP7.x提升了性能。2)PHP适合现代Web开发,广泛用于大型网站。3)PHP易学且服务器表现出色,但类型系统不如静态语言严格。4)PHP在内容管理和电商领域仍重要,生态系统不断进化。5)通过OPcache和APC等优化性能,使用OOP和设计模式提升代码质量。

PHP与Python辩论:哪个更好?PHP与Python辩论:哪个更好?Apr 16, 2025 am 12:03 AM

PHP和Python各有优劣,选择取决于项目需求。1)PHP适合Web开发,易学,社区资源丰富,但语法不够现代,性能和安全性需注意。2)Python适用于数据科学和机器学习,语法简洁,易学,但执行速度和内存管理有瓶颈。

PHP的目的:构建动态网站PHP的目的:构建动态网站Apr 15, 2025 am 12:18 AM

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

PHP:处理数据库和服务器端逻辑PHP:处理数据库和服务器端逻辑Apr 15, 2025 am 12:15 AM

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

您如何防止PHP中的SQL注入? (准备的陈述,PDO)您如何防止PHP中的SQL注入? (准备的陈述,PDO)Apr 15, 2025 am 12:15 AM

在PHP中使用预处理语句和PDO可以有效防范SQL注入攻击。1)使用PDO连接数据库并设置错误模式。2)通过prepare方法创建预处理语句,使用占位符和execute方法传递数据。3)处理查询结果并确保代码的安全性和性能。

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它们
4 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

DVWA

DVWA

Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

VSCode Windows 64位 下载

VSCode Windows 64位 下载

微软推出的免费、功能强大的一款IDE编辑器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用