WordPress分类模板无法显示自定义文章类型内容求助
Hey there! Let's break down why your custom post type (blog_post) listings aren't showing up on your taxonomy page. After reviewing your code, here are the key issues and fixes to get things working:
1. Mismatched Taxonomy Slug (Critical!)
Your template file is named taxonomy-blog_category.php, which tells WordPress it's built for the taxonomy with the slug blog_category. But in your tax_query, you're using 'taxonomy' => 'blog-category' (hyphen instead of underscore). WordPress matches taxonomy slugs exactly, so this mismatch is almost certainly the main reason no posts are loading.
2. Hardcoded Terms Instead of Dynamic Current Category
Right now you're forcing the query to pull posts from business, people, and technology—but when you visit site.com/blog_category/category, you want to show posts from that specific category in the URL. Instead of hardcoding terms, grab the current taxonomy term dynamically.
3. Verify Custom Post Type & Taxonomy Registration
Make sure your blog_post custom post type is registered to support the blog_category taxonomy. In your CPT registration code, you should have this line:
'taxonomies' => array( 'blog_category' ),
If this is missing, your posts won't be associated with the taxonomy at all.
Fixed Query Parameters
<div class="row"> <?php $ctr = 1; $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; // Get the current taxonomy term from the URL $current_term = get_queried_object(); $custom_args = array( 'post_type' => 'blog_post', 'orderby' => 'date', 'order' => 'DESC', 'post_status' => 'publish', 'posts_per_page' => 6, 'paged' => $paged, 'tax_query' => array( array( 'taxonomy' => 'blog_category', // Fixed slug to match template name 'field' => 'slug', 'terms' => $current_term->slug, // Dynamically use the current category ), ), ); ?>
Post Display Code (No core changes needed, included for context)
<?php $custom_query = new WP_Query( $custom_args ); ?> <?php if ( $custom_query->have_posts() ) : ?> <?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?> <div class="col-md-4 text-center"> <div class="content-container"> <div class="wrap"> <figure class="tint t2"> <img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240"> </figure> </div> <h2><?php the_title(); ?></h2> <h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3> <?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...'); ?> <div class="read-more-btn"> <a href="<?php the_permalink(); ?>">read more</a> </div> </div> </div> <?php $ctr++; endwhile; ?>
Fixed Pagination Code (Adjusted wp_reset_postdata() placement)
<div class="pagination-holder"> <ul class="pagination"> <?php if (function_exists('custom_pagination')) { custom_pagination($custom_query->max_num_pages,"",$paged); } wp_reset_postdata(); // Move this here to reset post data right after the loop ?> </ul> </div> <?php else: ?> <p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p> <?php endif; ?>
Quick Additional Checks
- Double-check that your
blog_postposts are actually assigned to theblog_categoryterms you're trying to view. - Ensure the
custom_paginationfunction properly uses the passedmax_num_pages(if it's a custom function, confirm it doesn't rely on the main WordPress query by accident).
内容的提问来源于stack exchange,提问作者Urek Mazino

