WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Need help adding list of posts by TAG.... (8 posts)

  1. kk4uga
    Member
    Posted 4 years ago #

    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!

  2. dragonsjaw
    Member
    Posted 4 years ago #

    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?

  3. MichaelH
    Member
    Posted 4 years ago #

    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().
    ?>
  4. kk4uga
    Member
    Posted 4 years ago #

    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.

  5. MichaelH
    Member
    Posted 4 years ago #

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

    See Function_Reference/get_terms for more arguments

  6. dragonsjaw
    Member
    Posted 4 years ago #

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

  7. kk4uga
    Member
    Posted 4 years ago #

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

  8. Grundlebug
    Member
    Posted 4 years ago #

    MichaelH --

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

Topic Closed

This topic has been closed to new replies.

About this Topic