[英]Listing post from custom post type category?
我有一個自定義帖子類型和創建的投資組合分類法,如下所示:
$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!
No posts were found. Sorry!
在我的頁面上。
該頁面是默認的wordpress存檔,應僅列出所有帖子,但什么都沒有顯示。 網址是這樣的:
http://xampp/my-theme/?portfolio-category=animals
但是,即使我使用名稱后的永久鏈接也沒有用。
我已經讀過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' );
但這也無濟於事。 我在這里想念什么?
您必須將存檔頁面模板用於自定義帖子類型。 並且請把代碼放在自定義帖子類型中
$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.