admin管理员组

文章数量:1293779

I'm learning Wordpress development on Udemy and currently developing a theme. I haven't added any custom taxonomy but just using the "Post" and "Categories". Found a tutorial of how to add "Featured Posts" I have created 3 categories, and I want to display featured post of that category when loads the category.php and avoid duplicated post.

The issue now is that the featured post (Ex. Category 1) is displaying on all category pages. Here is my code:

<?php
    $args = array(
        'posts_per_page' => 1,
        'meta_key' => 'meta-checkbox',
        'meta_value' => 'yes',
        'tax_query' => array(
            array(
                'taxonomy' => get_query_var('taxonomy'),
                'field' => 'slug',
                'terms' => get_query_var('term')
            )
        ),
    );
    $featured = new WP_Query($args);
    $targetID;

    if ($featured->have_posts()) :
        while ($featured->have_posts()) :
            $featured->the_post();
            $targetID = get_the_ID();
            get_template_part('template-parts/post/content', 'feature');
        endwhile;
    endif;

    if (have_posts()) {
        while (have_posts()) {
            the_post();
            if ($targetID == get_the_ID()) continue;
            get_template_part('template-parts/post/content');
        }
        the_posts_pagination();
        do_action('_themename_after_pagination');
    } else {
        get_template_part('template-parts/post/content', 'none');
    }
?>

本文标签: wp queryDisplay Featured Post by Categories and Avoid duplicated posts