WordPress.org

Ready to get started?Download WordPress

Forums

Displaying a "Post of the Day" on index.php (3 posts)

  1. Jon Griffith
    Member
    Posted 6 years ago #

    I am brainstorming a way to place a div above my content that would contain an "Article of the Day" based on the following criteria:

    The article would be shown from a specific category and it would be the most recent article in that category.

    If I miss a daily update, no big deal, the most recent post from that category will be displayed in the section.

    Is this a simple thing to do outside of the loop?

  2. lostgorilla
    Member
    Posted 6 years ago #

    Seems like this should be pretty simple.

    Use a plugin like WP-Recent-Posts or similar (thats 2.3 compatible) and call it with a template tag. Template tags for the most part (in my experience) will work just dandy outside the loop. Set the plugin for the cat you want and to pull the most recent post.

    Then its just a matter of setting up your div in the header. If you only say wanted it on the home page you could do that with a conditional IF ishome statement.

    Hope this helps.

  3. Kafkaesqui

    Posted 6 years ago #

    You can easily create a new post loop for displaying the latest post from a specific article (modify cat=10 to the correct category ID 3 for your 'Article of the Day' category):

    <?php
    $aotd = new WP_Query('showposts=1&cat=10');
    if( $aotd->post ) : $aotd->the_post();
    ?>
    
    <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php the_content(); ?>
    
    <?php endif; ?>

    (The above might be called a pseudo-loop; with just one being collected there's no reason to loop through the posts.)

    References:
    http://codex.wordpress.org/The_Loop#Multiple_Loops
    http://codex.wordpress.org/Template_Tags/query_posts
    http://codex.wordpress.org/Function_Reference/WP_Query

Topic Closed

This topic has been closed to new replies.

About this Topic