【问题标题】:Listing post from custom post type category?从自定义帖子类型类别中列出帖子?
【发布时间】:2015-10-01 13:31:44
【问题描述】:

我有一个自定义帖子类型和一个这样创建的投资组合分类:

    $args = array(
        'label' => __('Portfolio', 'my-portfolio'),
        'labels' => array(
            'add_new_item' => __('New portfolio', 'my-portfolio'),
            'new_item' => __('New portfolio', 'my-portfolio'),
            'not_found' => __('No portfolio items', 'my-portfolio'),
        ),
        'singular_label' => __('Portfolio', 'my-portfolio'),
        'menu_icon' => 'dashicons-portfolio',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'has_archive' => true,
        'exclude_from_search' => true,
        'show_in_nav_menus' => false,
        'supports' => array('title', 'editor', 'thumbnail'),
        'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
        'register_meta_box_cb' => 'add_remove_metaboxes_portfolio',
       );

    //Register type and custom taxonomy for type.
    register_post_type( 'portfolio' , $args );
    register_taxonomy("portfolio-category", array("portfolio"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Category", "rewrite" => true, "slug" => 'portfolio-category',"show_in_nav_menus"=>false));

当我在查询中使用它时,这就像它应该的那样工作。它显示在后端的菜单中,以及此自定义帖子类型的类别。

我的问题是,当我尝试从某个类别检索所有帖子时(通过单击类别名称),我的页面上出现了No posts were found. Sorry!

该页面是一个默认的 wordpress 存档,应该只列出所有帖子,但没有显示任何内容。网址是这样的:

http://xampp/my-theme/?portfolio-category=animals

但即使我使用帖子名称永久链接也没有任何作用。

我在CSS Tricks 上看到我可以在functions.php 中使用此功能

function namespace_add_custom_types( $query ) {
  if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
    $query->set( 'post_type', array(
     'post', 'portfolio', 'nav_menu_item'
        ));
      return $query;
    }
}
add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

但这也无济于事。我在这里错过了什么?

【问题讨论】:

  • 如果不打算使用父子关系,请在您的 cpt 中将分层设置为 false。
  • 我可能会使用父子关系,例如显示所有父类别,然后当您进入其中时,您可以看到子类别和帖子。

标签: php wordpress custom-post-type


【解决方案1】:

您必须为自定义帖子类型使用存档页面模板。并且请将代码放在自定义帖子类型中

$args = array(
        'label' => __('Portfolio', 'my-portfolio'),
        'labels' => array(
            'add_new_item' => __('New portfolio', 'my-portfolio'),
            'new_item' => __('New portfolio', 'my-portfolio'),
            'not_found' => __('No portfolio items', 'my-portfolio'),
        ),
        'singular_label' => __('Portfolio', 'my-portfolio'),
        'menu_icon' => 'dashicons-portfolio',
        'public' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => true,
        'has_archive' => true,
        'exclude_from_search' => true,
        'show_in_nav_menus' => false,
        'supports' => array('title', 'editor', 'thumbnail'),
        'rewrite' => array('slug' => 'portfolio', 'with_front' => false),
        'register_meta_box_cb' => 'add_remove_metaboxes_portfolio',
       );

    //Register type and custom taxonomy for type.
    register_post_type( 'portfolio' , $args );
flush_rewrite_rules();

【讨论】:

  • 不要以您的方式使用flush_rewrite_rules()。 Itnis 运行起来非常昂贵,而且需要大量资源。它会大大降低页面加载速度
  • 没有flush_rewrite_rules怎么可能调用页面模板?
  • 执行一次并删除它。法典列出了应该使用的钩子
  • 你和我写的有什么不同?除了flush_rewrite_rules();
  • 调用自定义帖子类型模板无需执行任何操作。
猜你喜欢
  • 2016-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 2014-07-10
  • 1970-01-01
  • 2011-06-24
  • 2013-05-12
相关资源
最近更新 更多