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
版权声明:本文标题:wp query - Display Featured Post by Categories and Avoid duplicated posts 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741577177a2386354.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论