【问题标题】:Latest 4 posts excluding category, append latest post from excluded category排除类别的最新 4 个帖子,附加排除类别的最新帖子
【发布时间】:2016-01-26 17:23:01
【问题描述】:

我需要制作一个“公告”横幅,显示 5 个最新帖子,其中一个 MUST 是最新的时事通讯。

我想做的是:最新的 4 篇不包括时事通讯类别的帖子 + 最新的时事通讯帖子作为第 5 个帖子。

这怎么可能?

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

我尝试从 newsletter 类别中排除所有帖子 ID,但如果最近 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>

有什么想法吗?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您可以创建两个单独的查询对象来实现您想要的。 例如

     //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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      相关资源
      最近更新 更多