从自定义帖子类型类别列出帖子?

问题描述:

我有一个自定义职位类型,这样创造了投资组合的分类:从自定义帖子类型类别列出帖子?

$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'); 

使用此功能,但是这并没有帮助。我在这里错过了什么?

+0

如果不打算使用父子关系,请在您的cpt中将等级设置为false。 –

+0

我可能会使用父子关系,就像显示所有父类别一样,然后当你进入其中时,你可以看到其中的子类别和帖子。 –

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

$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(); 
+0

不要以你所做的方式使用'flush_rewrite_rules()'。 Itnis运行起来非常昂贵,而且资源严重。它会降低页面加载的速度 –

+0

without flush_rewrite_rules如何调用页面模板? –

+0

做一次并将其删除。该手抄本列出了应该使用它的挂钩 –