0

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!

1 Answer 1

1

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"]
1
  • 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
    Commented Sep 9, 2011 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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