You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress分类模板无法显示自定义文章类型内容求助

Troubleshooting Your Custom Post Type Taxonomy Page Issue

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_post posts are actually assigned to the blog_category terms you're trying to view.
  • Ensure the custom_pagination function properly uses the passed max_num_pages (if it's a custom function, confirm it doesn't rely on the main WordPress query by accident).

内容的提问来源于stack exchange,提问作者Urek Mazino

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 03:54:20