$this->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', 'post', 'side', 'default' );

为了使插件与自定义帖子类型一起使用,我被告知将“帖子”更改为自定义帖子类型的名称。有人知道我是否可以与 全部 自定义帖子类型(包括常规帖子)通过以某种方式更改此行?

仅供参考,我在:http://wordpress.org/support/topic/custom-post-templates-with-custom-post-types-in-wp-30?replies=5#post-1679398

它是指自定义邮政模板插件:http://wordpress.org/extend/plugins/custom-post-template/

提前致谢!

编辑:

我试过了:

$post_types = get_post_types(array("public" => true));
foreach ($post_types as $post_type) {
  $this->add_meta_box("select_post_template", __("Post Template", "custom-post-templates"), "select_post_template", $post_type, "side", "default");
}

但是自定义帖子类型仍然没有获得模板选择菜单。这些帖子就像对原始代码一样所做的那样。感谢您的建议...有人有另一个吗?

注意:从概念上讲,方法是牢固的。如果我使用我的自定义帖子类型列表创建自己的数组,则此代码确实将模板添加到它们。

有帮助吗?

解决方案

您可以循环浏览所有注册的帖子类型,并为每个注册类型添加元框,尽管您可能需要过滤一些类型,因为附件也是帖子。

$post_types = get_post_types(array("public" => true));
foreach ($post_types as $post_type) {
  add_meta_box("select_post_template", __("Post Template", "custom-post-templates"), "select_post_template", $post_type, "side", "default");
}

关于专门针对自定义邮政模板插件的问题,我认为问题是您的自定义帖子类型在初始化后已注册(因为它不使用钩子)。所以, $post_types (上图)不包含您的类型,并且无法为它们添加元框。您可以尝试添加此黑客(在 custom-post-templates.php):

add_action('init', 'hack_add_meta_boxes');
function hack_add_meta_boxes() {
  global $CustomPostTemplates;
  $post_types = get_post_types(array('public' => true));
  foreach ($post_types as $post_type) {
    $CustomPostTemplates->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', $post_type, 'side', 'default' );
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top