搜尋
首頁後端開發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.如果您聽不到任何人,如何修復音頻
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.聊天命令以及如何使用它們
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

強大的PHP整合開發環境