简体   繁体   English

列出自定义帖子类型类别中的帖子?

[英]Listing post from custom post type category?

I have a custom post type and a taxonomy for portfolio created like this: 我有一个自定义帖子类型和创建的投资组合分类法,如下所示:

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

This works like it should when I use it in my queries. 当我在查询中使用它时,它应该像应该的那样工作。 And it shows in the menu in backend, along with the categories for this custom post type. 它显示在后端菜单中,以及此自定义帖子类型的类别。

My issue is when I try to retrieve all the post from a certain category (by clicking on the category name) I get No posts were found. Sorry! 我的问题是,当我尝试从某个类别(通过单击类别名称)检索所有帖子时,我得到No posts were found. Sorry! No posts were found. Sorry! on my page. 在我的页面上。

The page is a default wordpress archive that should just list all the posts, but nothing is showing. 该页面是默认的wordpress存档,应仅列出所有帖子,但什么都没有显示。 The url is like this: 网址是这样的:

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

But even if I use post-name permalinks nothing works. 但是,即使我使用名称后的永久链接也没有用。

I've read on CSS Tricks that I could use this function in functions.php 我已经读过CSS技巧 ,可以在functions.php使用此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' );

But this doesn't help either. 但这也无济于事。 What am I missing here? 我在这里想念什么?

You have to use archive page templates for your custom post type. 您必须将存档页面模板用于自定义帖子类型。 And Please put the code in custom post type 并且请把代码放在自定义帖子类型中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM