WordPress开发:单页按分类展示自定义文章类型内容
Got it, let's sort out this grouping problem for your career custom post type archive. The issue with repeated category titles happens because you’re probably outputting the category inside the post loop—instead, we need to loop through each career_category first, then list all posts under that category. Here’s a complete, working solution:
Step-by-Step Fixed Code for archive-career.php
Replace your current archive code with this; it’ll group posts by their career_category terms and only show each category title once:
<?php get_header(); ?> <div class="career-archive-container"> <h1><?php post_type_archive_title(); ?></h1> <?php // Fetch all non-empty career_category terms (adjust hide_empty if you want empty categories) $career_categories = get_terms(array( 'taxonomy' => 'career_category', 'hide_empty' => true, 'orderby' => 'name', 'order' => 'ASC' )); if (!empty($career_categories) && !is_wp_error($career_categories)) { // Loop through each career category foreach ($career_categories as $category) { // Output the category heading once per group echo '<h2 class="career-category-heading">' . esc_html($category->name) . '</h2>'; // Query only posts in this specific career category $category_posts = new WP_Query(array( 'post_type' => 'career', 'posts_per_page' => -1, // Show all posts in the category 'tax_query' => array( array( 'taxonomy' => 'career_category', 'field' => 'slug', 'terms' => $category->slug ) ), 'orderby' => 'title', // Sort posts by title (adjust to date if preferred) 'order' => 'ASC' )); // Display posts for this category if ($category_posts->have_posts()) { echo '<ul class="career-posts-list">'; while ($category_posts->have_posts()) { $category_posts->the_post(); ?> <li class="career-post-item"> <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3> <?php if (has_excerpt()) : ?> <p><?php the_excerpt(); ?></p> <?php endif; ?> </li> <?php } echo '</ul>'; // Reset post data to avoid conflicts with other queries wp_reset_postdata(); } else { // Optional: Show a message if no posts exist in the category echo '<p>No career posts found in this category.</p>'; } } } else { echo '<p>No career categories available.</p>'; } ?> </div> <?php get_footer(); ?>
Key Fixes & Notes
- Category-first loop: We start by fetching all
career_categoryterms, then loop through each one—this ensures each category title is only printed once, at the top of its post group. - Targeted post queries: For each category, we use
WP_Queryto pull only posts assigned to that specific category, avoiding the duplicate title issue from your original code. - Cleanup with
wp_reset_postdata(): This resets the global post object so other parts of your template (like the footer) don’t break. - Customizable sorting: Adjust the
orderbyandorderparameters in theWP_Queryif you want posts sorted by date instead of title.
Optional Styling
Add this CSS to your theme’s stylesheet to make the grouped layout look polished:
.career-category-heading { margin: 2.5rem 0 1rem; padding-bottom: 0.75rem; border-bottom: 2px solid #e0e0e0; color: #2d3748; } .career-posts-list { list-style: none; padding-left: 0; } .career-post-item { margin-bottom: 1.5rem; padding: 1.25rem; background-color: #f7fafc; border-radius: 6px; } .career-post-item h3 { margin-top: 0; }
内容的提问来源于stack exchange,提问作者Zain Shabir
相关产品推荐
相关产品推荐

