代码之家  ›  专栏  ›  技术社区  ›  Matrym

如何包含所有自定义帖子类型而不是只包含帖子

  •  0
  • Matrym  · 技术社区  · 15 年前
    $this->add_meta_box( 'select_post_template', __( 'Post Template', 'custom-post-templates' ), 'select_post_template', 'post', 'side', 'default' );
    

    为了使一个插件能够与自定义的post类型一起工作,我被告知要将“post”改为自定义post类型的名称。有人知道我能不能和你一起工作吗 全部的 通过某种方式更改此行来自定义文章类型(包括常规文章)?

    仅供参考,我在: 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类型仍然没有得到模板选择菜单。帖子会这样做,就像他们对原始代码所做的那样。谢谢你的建议。。。有人有别的吗?

    注:从概念上讲,这种方法是可靠的。如果我用自定义post类型的列表创建自己的数组,那么这段代码会向它们添加模板。

    1 回复  |  直到 15 年前
        1
  •  1
  •   Richard M    15 年前

    您可以遍历所有已注册的post类型并为每个类型添加meta框,尽管您可能需要过滤掉某些类型,因为附件也是post。

    $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模板插件,我认为问题在于,自定义Post类型在初始化之后就被注册了(因为它不使用钩子)。所以, $post_types (上图)不包含您的类型,无法为它们添加元框。你可以尝试添加这个hack(在 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' );
      }
    }
    
    推荐文章