• Resolved kk4uga

    (@kk4uga)


    I am trying to display an unordered list of posts by TAG, not category. On one of my sites, I have a list by CATEGORY as follows:

    <ul>
    <?php
    $posts=get_posts('cat=13&showposts=50');
    if ($posts) {
    foreach($posts as $post) {
    setup_postdata($post);
    printf ("<li><a href=\"%s\">%s</a></li>", get_permalink($post->ID),$post->post_title);
    ?>
    <?php }
    }
    ?>
    </ul>

    Is it possible to modify this code to display a list of posts by TAG instead of CATEGORY?

    Thanks for any help!

Viewing 7 replies - 1 through 7 (of 7 total)
  • I use this on a page, it only lists the Tag name which is linked to a page with all the post titles using that tag.

    ‘<?php wp_tag_cloud( ‘format=list&orderby=count&order=DESC&smallest=8&largest=8′ ); ?>’

    the 8 and 8 means 8 pixels for the display size.
    Some of the looks of this will be controlled by your CSS for tags.

    Maybe this is a start.. you could add in the get posts and permalink stuff?

    list all posts sorted by tag name or category name

    <?php
    //get all terms (e.g. categories or post tags), then display all posts in each term
    $taxonomy = 'post_tag';//  e.g. post_tag, category
    $param_type = 'tag__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => 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 '.$taxonomy .' '.$term->name;
          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().
    ?>

    Thread Starter kk4uga

    (@kk4uga)

    Thanks for the help.

    MichaelH, that code works for me, but is there a way to only display one particular tag? Right now, they all show up which is way too many.

    $term_args=array(
      'include' => '1,2,3',
      'orderby' => 'name',
      'order' => 'ASC'
    );

    See Function_Reference/get_terms for more arguments

    sorry i misread you original question thinking you wanted a list of tags.

    Thread Starter kk4uga

    (@kk4uga)

    Thanks guys, that works. Appreciate the help as I am not a coder.

    MichaelH —

    This code seriously helped me out. Thanks for making it so flexible too.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Need help adding list of posts by TAG….’ is closed to new replies.