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

如何按分类法分组显示自定义文章类型的摘要、缩略图和标题?调试特定分类下标题显示代码

Hey there! Let's break down solutions for both of your WordPress customization needs, starting with refining the code you've already worked on.

需求2:显示特定自定义文章类型及分类法下的所有文章标题

Your initial code has the right core idea, but we can tweak it to be more robust and avoid potential conflicts with global variables. Here's an optimized version:

<?php
// Save the original global $post to avoid conflicts later
global $post;
$original_post = $post;

$myposts = get_posts(array(
    'post_type'      => 'my-post-type',
    'tax_query'      => array(
        array(
            'taxonomy' => 'post-type-category',
            'field'    => 'slug', // Explicitly define the field (avoids ambiguity with term IDs)
            'terms'    => 'new-posts'
        )
    ),
    'posts_per_page' => -1 // Fetch ALL matching posts (default is 10)
));

if (!empty($myposts)) {
    echo '<ul class="category-posts">';
    foreach ($myposts as $post) {
        setup_postdata($post); // Enable WordPress template functions like get_the_title()
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
    wp_reset_postdata(); // Restore the original global $post variable
} else {
    echo '<p>No posts found in this category.</p>';
}

// Restore original post if needed elsewhere in your template
$post = $original_post;
?>

Key improvements:

  • Added posts_per_page => -1 to ensure you get every post matching your criteria (not just the first 10)
  • Specified field => 'slug' in the tax query to clarify you're targeting the term slug (prevents mismatches if you were accidentally using term IDs)
  • Used setup_postdata() and wp_reset_postdata() to handle the global $post variable safely, which prevents issues with other template functions later
  • Wrapped titles in an unordered list for cleaner, semantic HTML
  • Added a fallback message for when no posts are found
需求1:按分类法分组显示自定义文章类型的摘要、缩略图和标题

To display posts grouped by their taxonomy terms, we'll first fetch all relevant terms, then loop through each term to pull and display its associated posts. Here's a complete implementation:

<?php
// Define your custom post type and taxonomy
$custom_post_type = 'my-post-type';
$target_taxonomy = 'post-type-category';

// Get all non-empty terms from the taxonomy
$terms = get_terms(array(
    'taxonomy'   => $target_taxonomy,
    'hide_empty' => true, // Only show terms that have posts
));

if (!empty($terms)) {
    foreach ($terms as $term) {
        // Display the term name as a group header
        echo '<h2 class="taxonomy-group-header">' . $term->name . '</h2>';
        
        // Fetch posts in this term
        $group_posts = get_posts(array(
            'post_type'      => $custom_post_type,
            'tax_query'      => array(
                array(
                    'taxonomy' => $target_taxonomy,
                    'field'    => 'term_id',
                    'terms'    => $term->term_id
                )
            ),
            'posts_per_page' => -1
        ));
        
        if (!empty($group_posts)) {
            echo '<div class="post-group-container">';
            foreach ($group_posts as $post) {
                setup_postdata($post);
                echo '<article class="post-card">';
                // Display post thumbnail (if available)
                if (has_post_thumbnail()) {
                    echo '<div class="post-thumbnail">' . get_the_post_thumbnail($post->ID, 'medium') . '</div>';
                }
                // Display post title with link
                echo '<h3 class="post-title"><a href="' . get_permalink() . '">' . get_the_title() . '</a></h3>';
                // Display post excerpt
                echo '<div class="post-excerpt">' . get_the_excerpt() . '</div>';
                echo '</article>';
            }
            echo '</div>';
            wp_reset_postdata();
        } else {
            echo '<p>No posts found in this group.</p>';
        }
    }
} else {
    echo '<p>No taxonomy terms found for this post type.</p>';
}
?>

Important notes:

  • We use get_terms() to fetch all valid terms first, which acts as our grouping structure
  • Each term's posts are queried individually to ensure strict grouping
  • get_the_post_thumbnail() uses the 'medium' size by default—you can replace this with 'thumbnail', 'large', or a custom image size you've registered
  • The post title is wrapped in a link to the full post for better UX
  • get_the_excerpt() will use a manually set excerpt if available; if not, it auto-generates a 55-character snippet from the post content
  • Added semantic CSS classes to make styling easier (you can customize these to match your theme)
  • Fallback messages handle cases where no terms or posts exist, so your page won't have empty gaps

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 07:46:22