WordPress 自定义 Post 类型类别页面未加载帖子
WordPress Custom Post Types Category page not loading posts
我创建了一个名为制造商的自定义 post 类型,并添加了大量 post 和类别。单个 post 页面有效,但类别/存档页面不显示任何 post。
制造商分为不同的类别,我需要显示每个类别中所有 post 的存档。当我转到 Manufactures>Category>Ge Speedtronic 并单击 "view category" 时,它会将我带到下面列出的 url。
http://localhost/category/manufactures/ge-speedtronic/
这就是令人困惑的地方。我用于自定义 post 类型 "Manufactures" 的类别也显示在我的其他自定义 post 类型和默认 post 部分下。所以我创建了一个名为 Manufactures-Cat 的类别,其中包含子类别。这可能是导致问题的原因。
知道类别页面上没有加载哪些 post 吗?是否可以创建仅在特定 post 类型下显示的类别?
此代码在我的 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(); ?>
您必须将自定义 post 类型添加到查询对象:
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
我创建了一个名为制造商的自定义 post 类型,并添加了大量 post 和类别。单个 post 页面有效,但类别/存档页面不显示任何 post。
制造商分为不同的类别,我需要显示每个类别中所有 post 的存档。当我转到 Manufactures>Category>Ge Speedtronic 并单击 "view category" 时,它会将我带到下面列出的 url。
http://localhost/category/manufactures/ge-speedtronic/
这就是令人困惑的地方。我用于自定义 post 类型 "Manufactures" 的类别也显示在我的其他自定义 post 类型和默认 post 部分下。所以我创建了一个名为 Manufactures-Cat 的类别,其中包含子类别。这可能是导致问题的原因。
知道类别页面上没有加载哪些 post 吗?是否可以创建仅在特定 post 类型下显示的类别?
此代码在我的 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(); ?>
您必须将自定义 post 类型添加到查询对象:
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