首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >最新4个员额(不包括职类),从排除在外职类中追加最新员额

最新4个员额(不包括职类),从排除在外职类中追加最新员额
EN

Stack Overflow用户
提问于 2016-01-26 08:55:13
回答 1查看 35关注 0票数 0

我需要建立一个‘公告’横幅显示5个最新的帖子,其中一个MUST是最新的通讯。

我想做的是:最新的4篇文章,不包括时事通讯类+最新的时事通讯文章作为第5篇。

这怎麽可能?

我尝试过tax_query,但是它只显示了newsletter类别,并且没有限制该特定类别。

我试过将所有的帖子it排除在newsletter类别之外,但是如果在最新的5篇文章中没有时事通讯,它根本就不会显示最新的时事通讯。

代码:

代码语言:javascript
复制
$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>

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-26 09:23:01

您可以创建两个独立的查询对象来实现您想要的结果。e.g

代码语言:javascript
复制
 //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 article
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35010009

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档