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!
on my page.
The page is a default wordpress archive that should just list all the posts, but nothing is showing. 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
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.