简体   繁体   中英

How to display 3 previous posts in article page?

Does anyone know how to display 3 previous posts on article page? Let's say I'm in article post page and would like to display previous 3 posts images (basing on the post I'm currently reading) below it's content, but not next posts images . I was trying to do this with setting up the offset in the db query but with no luck. Can someone please help me with this one? I would greatly appreciate any help or at least pointing me in the right direction.

That shouldn't be too hard, if you use an id field with auto increment values, you could do something like this:

SELECT id, name FROM articles WHERE id < {current_id} ORDER BY id DESC LIMIT 3;

Obviously, replace {current_id} with the id of the article you're currently reading.

After displaying the specific post do a new WP_Query to retrieve the 3 posts previous to the publication date of the displayed post. This documentation page describes how to query for posts with a relative date (eg the example Return posts from the last 30 days ). Base the query on the publication time of the displayed post.

The example includes a way to supply a WHERE clause to the query, with add_filter(). I'm not sure, but I think you need to call remove_filter after doing the query, or that filter might be applied to other queries also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM