• macuache

    (@macuache)


    I need to list posts with tags as headlines on a page by this pattern:

    Tag 1

    * Post 1
    * Post 2
    * Post 3

    Tag 2

    * Post 4
    * Post 5
    * Post 6

    Tag 3

    * Post 7
    * Post 8
    * Post 9

    As the number of tags will increase, I can’t just put “<?php query_posts(‘tag=cooking’); ?>”

Viewing 1 replies (of 1 total)
  • MichaelH

    (@michaelh)

    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().
    ?>

Viewing 1 replies (of 1 total)
  • The topic ‘How to list posts with tags as headlines (example)’ is closed to new replies.