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

php - How to apply 'add two more posts' to media content?

programmeradmin1浏览0评论

Goal: on media pages (image), locate the original post the images were added to (Wordpress already does this) and then print the two previous articles.

This code prints the two previous articles underneath a given POSTS Add to previous posts under post

However, I also want to apply this function to WordPress MEDIA, so that underneith the media image page, the two posts that were published before the article the image is from are printed.

ie

Media image (from article 17)
Article 16 (complete as it would be if viewing the post)
Article 15 (complete as it would be if viewing the post)

Although I'm not sure if this is even possible with WordPress.

Goal: on media pages (image), locate the original post the images were added to (Wordpress already does this) and then print the two previous articles.

This code prints the two previous articles underneath a given POSTS Add to previous posts under post

However, I also want to apply this function to WordPress MEDIA, so that underneith the media image page, the two posts that were published before the article the image is from are printed.

ie

Media image (from article 17)
Article 16 (complete as it would be if viewing the post)
Article 15 (complete as it would be if viewing the post)

Although I'm not sure if this is even possible with WordPress.

Share Improve this question asked Mar 20, 2019 at 18:02 MantaRayMantaRay 254 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

First, modify your example by specifying post type post and removing the cat and post__not_in arguments. Next, if you want to make sure you're looking at the parent's date and not the media's date, you'll also need to make sure to have a fallback in case someone tries to view an image that isn't attached to a post (and therefore doesn't have a parent).

// Make sure we can access the current $post, which is media
global $post;
// If the media isn't attached
if($post->post_parent == 0) {
    // Use the media itself's upload date
    $date = $post->post_date;
} else {
    // Get the parent post
    $parent = get_post($post->post_parent);
    // Use its date
    $date = $parent->post_date;
}
// Get 2 Posts
$qry = new WP_Query(
    array(
        // This pulls exactly 2 posts
        'posts_per_page' => 2,
        // This pulls only Posts
        'post_type' => 'post',
        // This finds posts published before the current item
        'date_query' => array(
            array(
                'before' => $date,
            ),
        ),
    )
);

You'll then need to do something with the results. You can start with a simple print_r($qry); to make sure you retrieved the posts you intended to, then move on to a custom loop to actually display them:

if($qry->have_posts()):
    while($qry->have_posts()) : $qry->the_post();
        // Set up whatever html structure you want here ?>
        <hr/>
            <article>
            <h2><?php the_title(); ?></h2>
            <?php the_content(); ?>
        </article><?php
    endwhile;
endif;

This will create a horizontal rule, then show the title and content of the first post, then another rule, then title and content of the second post.

发布评论

评论列表(0)

  1. 暂无评论