2017-02-16 142 views
1

我創建了一個名爲Manufactures的自定義帖子類型,並添加了大量的帖子和類別。單個帖子頁面可以工作,但類別/存檔頁面不顯示任何帖子。WordPress自定義帖子類型分類頁面未加載帖子

製造商分爲不同類別,我需要顯示每個類別中所有帖子的存檔。當我去製造商>類別> Ge Speedtronic並點擊「查看類別」時,它會將我帶到下面列出的網址。

http://localhost/category/manufactures/ge-speedtronic/

這是它得到混亂。我用於自定義帖子類型「製作品」的類別也顯示在我的其他自定義帖子類型和默認帖子部分下。所以我創建了一個名爲Manufactures-Cat的分類。這可能會導致問題。

任何想法哪些帖子不在類別頁面上加載?是否有可能創建只顯示在特定帖子類型下的類別?

此代碼是我的functions.php

add_action('init', 'create_manufacturers'); 
function create_manufacturers() { 
    register_post_type('manufacturers', 
    array(
     'labels' => array(
     'name' => __('Manufacturers'), 
     'singular_name' => __('Manufacturer') 
    ), 
     'hierarchical'  => true, 
     'public'    => true, 
     'show_ui'    => true, 
     'show_in_menu'  => true, 
     'show_in_nav_menus' => true, 
     'show_in_admin_bar' => true, 
     'menu_position'  => 5, 
     'can_export'   => true, 
     'has_archive'   => true, 
     'has_category'   => true, 
     'exclude_from_search' => false, 
     'publicly_queryable' => true, 
     'capability_type'  => 'post', 
     'taxonomies' => array('category'), 
     'rewrite' => array('slug' => 'manufacturers'), 
     'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments') 
    ) 
); 
} 

這是我category.php文件中的代碼。

<?php get_header();?> 
<div class="container page-category-wrap"> 
    <div class="row"> 
     <div class="col-md-9"> 
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
      <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
       <div class="entry-content"> 
        <h3><?php the_title(); ?></h3> 
        <?php the_content(); ?> 
       </div> 
      </article> 
      <?php endwhile; endif; ?> 
     </div> 
     <div class="col-md-3"> 
      <?php dynamic_sidebar ('sidebar-1');?> 
     </div> 
    </div> 
</div> 
<?php get_footer(); ?> 

回答

1

你有你的自定義後類型添加到查詢對象:

function my_query_post_type($query) { 
    if (is_category() && (! isset($query->query_vars['suppress_filters']) || false == $query->query_vars['suppress_filters'])) { 
     $query->set('post_type', array('post', 'manufacturers')); 
     return $query; 
    } 
} 
add_filter('pre_get_posts', 'my_query_post_type'); 

https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

+0

感謝您的幫助!這工作,我現在在頁面上看到帖子。在調試模式下,我收到此消息... 未定義的索引:第177行的suppress_filters if(is_category()&& false == $ query-> query_vars ['suppress_filters']){ –

+0

我已更新我的回答。這應該避免警告消息。 –

+0

工作,謝謝! –

相關問題