WordPress.org

Ready to get started?Download WordPress

Forums

Show object in last post only. (3 posts)

  1. blazemiskulin
    Member
    Posted 8 years ago #

    I have a two-part question:

    For a theme I'm designing, I came up with the idea of having an image (such as a webcam) that only appears in the last post. The trick is that it needs to be *inside* the post content so that the text can wrap around it. I imagine this could be accomplished by a simple if/then statement; basically "if (post is most recent) then (show object)". The problem I have is how to determine if a post is the most recent.

    I have a php coder working on some stuff for me (I do css, not php) but he's brand new to WP, so I thought I'd ask a few questions first to help him out.

    First: Is there an existing plugin that accomplishes what I want: inserting an object (image) into only the the most recent post?

    Second: If not, what is the most efficient way to determine if a post is the most recent *and* be able to manipulate that variable in either a hard-coded statement or a plugin (preferred)?

    I would also want to have a way to either wrap a css class around the code, or set its parameters in the plugin (height, width, border, etc.), if that makes any difference.

    Thanks.

  2. Mark Jaquith
    WordPress Lead Dev
    Posted 8 years ago #

    Well, you could use get_posts() to grab the most recent post and look at its ID

    <?php
    function add_to_most_recent_post($text) {
    global $post;
    $most_recent_post = get_posts('numberposts=1');
    if ($post->ID == $post_recent_post->ID)
    $text = "<p><strong>Most Recent Post!</strong></p>" . $text;
    return $text;
    }
    add_filter('the_content', 'add_to_most_recent_post', 30);
    ?>

    Obviously, get_posts() is probably not the most efficient way, as you are grabbing the entire post object when you just want the ID. But that should give you an idea of how you could use the "the_content" filter to adjust the text of an entry.

  3. blazemiskulin
    Member
    Posted 8 years ago #

    Where would that code go?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.