我需要建立一个‘公告’横幅显示5个最新的帖子,其中一个MUST是最新的通讯。
我想做的是:最新的4篇文章,不包括时事通讯类+最新的时事通讯文章作为第5篇。
这怎麽可能?
我尝试过tax_query,但是它只显示了newsletter类别,并且没有限制该特定类别。
我试过将所有的帖子it排除在newsletter类别之外,但是如果在最新的5篇文章中没有时事通讯,它根本就不会显示最新的时事通讯。
代码:
$category_id = get_cat_ID('Newsletter');
$latest_newsletter = query_posts("cat=$category_id&order_by=date");
$exclude_ids = [];
$count = 0;
while (have_posts()) : the_post();
$post = get_post();
if($count > 0) {
$exclude_ids[] = $post->ID;
}
$count++;
endwhile;
wp_reset_query();
query_posts(['posts_per_page' => '4', 'order_by' => 'date', 'post__not_in' => $exclude_ids]); if (have_posts()) : ?>
<div class="news-announcement">
<h3>ANNOUNCEMENT</h3>
<div id="ticker" class="ticker">
<ul>
<?php while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="Continue reading"> continue reading...</a>
<?php the_title( ); ?></li>
<?php endwhile; ?>
</ul>
</div>
</div>有什么想法吗?
发布于 2016-01-26 09:23:01
您可以创建两个独立的查询对象来实现您想要的结果。e.g
//this is just an example not complete code
// write you actual query in the array passed to WP_Query
$latest_articles = new WP_Query( array('order' => 'DESC', 'posts_per_page ' => 4) );
while ( $latest_articles->have_posts() ) :
// Loop over the posts and show the content
$newletter_article = new WP_Query( array('order' => 'DESC', 'posts_per_page'=> 1, 'cat' => 1 ) );
while ( $newletter_article->have_posts() ) :
//show the newletter articlehttps://stackoverflow.com/questions/35010009
复制相似问题