Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a plugin request that I cannot find by a Google search because I can't think of what it would be called:

I need a plugin that will, on a page-by-page basis, allow me to define a list of tags (probably via a Custom Field), and should show the most recent 5 (or any number) posts that have that tag on the bottom of the page.

Thanks!

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Here's a quick shortcode I put together for your functions.php file if you want to use it as a starting point. or it can be converted to a plugin if you prefer.

function wpse27961_show_tagged_posts( $atts ) {
    extract( shortcode_atts( array(
        'tags' => '',
        'number' => 5,
    ), $atts ) );

    $args = array(
        'posts_per_page' => $number,
        'tag' => $tags
    );

    $wpse_tagged_posts = new WP_Query( $args );

    $output = "";

    if($wpse_tagged_posts):
        $output .= "<ul>";
        while($wpse_tagged_posts->have_posts()):
            $wpse_tagged_posts->the_post();
            $output .= '<li><a href="';
            $output .= get_permalink();
            $output .= '">';
            $output .= the_title( '', '', false );
            $output .= '</a></li>';
        endwhile;
        $output .= "</ul>";
    endif;

    wp_reset_query();

    return $output;
}
add_shortcode( 'wpse_tagged_posts', 'wpse27961_show_tagged_posts' );

yeah, it's not pretty, but someone can tweak it if they have the time. add the shortcode to your page like this:

[wpse_tagged_posts tags="foo,bar"]

it'll default to showing 5 posts. or if you want a specific number of posts:

[wpse_tagged_posts tags="foo,bar" number="6"]
share|improve this answer
    
That is great! May I suggest one thing - add a wp_reset_query(); after the "endif;" so the "edit" link that appears on pages when you're logged in works correctly. –  GavinR Sep 9 '11 at 0:59
    
Thanks, updated. –  Milo Sep 9 '11 at 1:24
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.