最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Latest posts feed with a specific post always first

programmeradmin0浏览0评论

I have a latest posts slider working nicely. I would like to push a specific post (ID 123) to always appear first. I believe I could run multiple queries but every attempt has failed. I'm running out of ideas and steam! So, the featured post first, then the latest posts by date (not a problem if the featured post is duplicated when shown in date order.

<div class="feed">
    <h2>Latest</h2>
    <div id="feed" class="owl-carousel">
        <?php $args = array( 'numberposts' => 10, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
        $postslist = get_posts( $args );
        foreach ($postslist as $post) : setup_postdata($post); ?>
        <div class="inner">
           <h3><?php the_title(); ?></h3>
           <?php the_excerpt(); ?>
           <a href="<?php the_permalink(); ?>" title="<?php the_title();?>">More</a>
        <div>
        <?php endforeach; ?>
    </div>
</div>

I have a latest posts slider working nicely. I would like to push a specific post (ID 123) to always appear first. I believe I could run multiple queries but every attempt has failed. I'm running out of ideas and steam! So, the featured post first, then the latest posts by date (not a problem if the featured post is duplicated when shown in date order.

<div class="feed">
    <h2>Latest</h2>
    <div id="feed" class="owl-carousel">
        <?php $args = array( 'numberposts' => 10, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");
        $postslist = get_posts( $args );
        foreach ($postslist as $post) : setup_postdata($post); ?>
        <div class="inner">
           <h3><?php the_title(); ?></h3>
           <?php the_excerpt(); ?>
           <a href="<?php the_permalink(); ?>" title="<?php the_title();?>">More</a>
        <div>
        <?php endforeach; ?>
    </div>
</div>
Share Improve this question asked Jul 15, 2019 at 14:48 Gareth RobertsGareth Roberts 1
Add a comment  | 

1 Answer 1

Reset to default 0

Since you're using get_posts() rather than WP_Query, it's sufficient to retrieve the specific post separately with get_post(), and then add it to the beginning of the results array with array_unshift():

$postslist     = get_posts( $args );
$featured_post = get_post( 123 );

array_unshift( $postslist, $featured_post );

foreach ( $postslist as $post ) : setup_postdata($post);
发布评论

评论列表(0)

  1. 暂无评论