ホームページ  >  記事  >  バックエンド開発  >  カスタム投稿タイプ用の多用途プラグインを作成する

カスタム投稿タイプ用の多用途プラグインを作成する

王林
王林オリジナル
2023-08-27 08:01:17744ブラウズ

多くの場合、特定の機能を実装する方法を選択して決定する必要があります。 WordpPress は、ほぼすべての問題に対してかなり柔軟なアプローチを持っています。このチュートリアルでは、カスタム投稿タイプ API の柔軟性を見ていきます。

カスタム投稿タイプを使用すると、特定のカスタム ページ、スライドショー、ギャラリー、さらにはポートフォリオ プロジェクトで特定の投稿を生成および管理するなど、さまざまなことができます。それらの存在をまだ知らない人もいれば、そのオプションとその使用方法を完全に理解していない人もいます。次の手順で、カスタム投稿タイプで最適に動作する特定のプラグインを構築してみます。より正確には、他のカスタム投稿タイプ (ここでは Inception など) を動的に作成および管理する 1 つのカスタム投稿タイプを作成するプラグインを構築します。


ステップ 1計画

始める前に、このチュートリアルで何を、どのように、そしてなぜ構築するのかを詳しく見ていきます。このチュートリアルの目的は、WordPress のカスタム投稿タイプに慣れるのに役立つことです。

これを行うには、メインのカスタム投稿タイプを作成し、そのカスタム投稿タイプ内の投稿を使用するプラグインを構築し、メタ ボックスのインターフェース オプションに基づいて他の動的なカスタム投稿タイプを作成するメタ ボックスを設定します。 。これは、すべてのカスタム投稿オプションとそれが提供できるいくつかの可能性について詳しく知るのに役立ちます。このチュートリアルでは、PHP と MySQL を実行する Apache サーバーが稼働しており、WordPress バージョン 3.0 でプラグイン コードをテストできるように WordPress がインストールされていることを前提としています。

各投稿は新しいカスタム投稿タイプとして解釈され、メタ ボックスを使用して、多数のフォーム、チェックボックス、ドロップダウン、テキスト ボックス、チェックボックス、ドロップダウン ボックスを備えたカスタム フィールドに各投稿の構成オプションを保存します。 、およびテキスト ボックスはデータを保存および表示するため、ユーザーはあらゆるものを簡単に変更できます。

プラグインには 450 行のコードがあるため、コードのほとんどが異なる変数を使用して繰り返されるため、チュートリアルにはコードのすべての行が表示されるわけではありません。ただし、プラグインのすべての機能については、ソース コードの例を使用して説明します。


ステップ 2フックを作成して理解する

カスタム投稿タイプ機能やメタボックス機能を取得し、更新または公開操作でデータを保存したい場合は、フックが必要であり、望ましい効果を得るためにフックを具体的に実装する必要があります。

リーリー

このチュートリアルには 3 つのフックのみが必要で、フィルターはなく、コア機能には他に何も必要ありません。まず、コールバック関数でカスタム投稿タイプを生成するためのフックが必要です。これを行うには、init タグ フックと 'init_custom_post_types' という名前のコールバック関数を使用します。ここですべての魔法が起こります。つまり、カスタム投稿タイプを宣言し、データベースから値を取得してループ内で動的カスタム投稿タイプを生成することで、カスタム投稿タイプを動的にします。コードのこの部分については後で説明します。

必要なもう 1 つのフックは、'add_meta_boxes' タグ フックです。これは、他のすべてのカスタム マスター カスタム投稿タイプを生成するときに使用されるコールバック関数 'cpt_add_meta_boxes' を指します。メタボックスポストタイプ。ここで、機能を作成するために操作する必要があるデータを保存および更新するフォームを作成します。

最後に使用するフックは 'save_post' で、そのコールバック関数は 'cpt_save_postdata' で、メタ ボックスによってポストされたデータを保存するために使用されます。ユーザーが管理パネルでメインのカスタム投稿タイプでページを送信すると、すべてのフォーム フィールドが送信されます。これは、そのコンテンツを取得し、後で使用できるようにデータベースに保存/更新するために使用するフックです。


ステップ 3プライマリカスタム投稿タイプを作成する

まず、すべての動的コンテンツを処理するための共通のカスタム投稿タイプを作成する必要があります。これを行うには、まず、以下の例に示すように、ハードコーディングされたデータを含む汎用のカスタム投稿タイプを作成します。まずはラベルを作成します。ラベル パラメーターは、配列として解釈される主要なパラメーター値の 1 つです。配列値はその識別子によって与えられます。具体的には、name はカスタム投稿タイプの主な共通名を表し、通常はカスタム投稿タイプ名の複数形の文字列を表し、singular_name は単数形の Custom を表します。投稿フォームにオブジェクト名を入力します。新しい文字列テキストを追加するには 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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。