2015-07-13 189 views
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); 
    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

我可能會使用父子關係,就像顯示所有父類別一樣,然後當你進入其中時,你可以看到其中的子類別和帖子。 –

回答

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

做一次並將其刪除。該手抄本列出了應該使用它的掛鉤 –

相關問題