最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far