• Hi guys,
    I have a number of posts in a sub-category, approximately 100 in total. I want to use the 6 or so most popular posts and link to them as a menu.

    I’m hoping I can do this by tagging the 6 posts with a tag such as “important” and then use some form of query or loop to list all posts tagged with “important”.

    Is this possible?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Well, natively, when a user clicks on a tag link presented via a Tag Cloud Widget, or a display the_tags with each of your posts, then WordPress will automatically list all the posts with that tag.

    See Tag Templates, Template Hierarchy, the_tags(), and wp_tag_cloud().

    But this could be used in a template such as a sidebar.php or page template:

    <?php
    $term = get_term_by('slug','important', 'post_tag');
    $args=array(
      'tag__in' => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo 'List of Posts in tag '. $term->description;
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    is there a way to limit to show only 5 on the list rather than every post under a specific tag?

    Try using:

    $args=array(
      'tag__in' => array($term->term_id),
      'post_type' => 'post',
      'post_status' => 'publish',
      'posts_per_page' => 5,
      'caller_get_posts'=> 1
    );
Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘List Posts by Tag’ is closed to new replies.