WordPress自定义帖子类型分类页面未加载帖子
问题描述:
我创建了一个名为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(); ?>
答
你有你的自定义后类型添加到查询对象:
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
感谢您的帮助!这工作,我现在在页面上看到帖子。在调试模式下,我收到此消息... 未定义的索引:第177行的suppress_filters if(is_category()&& false == $ query-> query_vars ['suppress_filters']){ –
我已更新我的回答。这应该避免警告消息。 –
工作,谢谢! –