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

Goal: on media pages (image), locate the original post the images were added to (Wordpress already does this) and then p

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745671289a4639417.html

相关推荐

  • php - How to apply &#39;add two more posts&#39; to media content?

    Goal: on media pages (image), locate the original post the images were added to (Wordpress already does this) and then p

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信