ThemeShaper Forums » Thematic

[closed]

Create post list grouped by tag

(3 posts)
  • Started 11 years ago by bronwyn
  • Latest reply from bronwyn
  • This topic is resolved
  1. bronwyn
    Member

    I am trying to make a custom loop to do this:

    Tag Name 1
    post 1
    post 2
    Tag Name 2
    post A
    post B

    For each tag, I want to have a tag name header (linked to the tag archive page) and then two posts.

    I have been trying to use this code:


    $taglist = get_terms('post_tag', 'hierarchical=0&orderby=name&hide_empty=1'); foreach ($taglist as $pftag) {
    query_posts("tag_id=$pftag->term_ID&posts_per_page=2&orderby=date");?>
    <h3><?php print_r($pftag->name); ?></h3>
    <?php while (have_posts()) : the_post();
    purplefrog_excerpt_tile();
    endwhile; }

    The posts returned by query_posts are not filtered by tag at all. It just pops up the most recent 2 posts over and over. What am I doing wrong, and why is this so %$#^ing hard? I've been working on this for about 4 hours with very little progress, even though I got the categories version of this working much faster.

    The print_r for the tag name header is messy, but I haven't found anything else that actually outputs the tag name for *all* entries...

    Posted 11 years ago #
  2. bronwyn
    Member

    The problem is solved thanks to the kindness of alchymyth over on the Wordpress forums. In case anyone else wants to know how it is done, it goes like this:


    // get the tag list
    $taglist = get_terms('post_tag', 'hierarchical=0&orderby=name&hide_empty=1');
    // iterate through the tag list
    foreach ($taglist as $pftag)
    {
    //get 2 posts belonging to the tag -- ONLY the slug works to filter by tag
    query_posts('tag='.$pftag->slug.'&posts_per_page=2&orderby=date');?>
    // make a linked heading with the tag name
    <h3>slug); ?>"><?php echo($pftag->name); ?></h3>
    // make the actual loop for the posts, which outputs them using a custom action, because I use this format many places
    <?php while (have_posts()) : the_post();
    purplefrog_excerpt_tile();
    endwhile;
    }

    Posted 11 years ago #
  3. bronwyn
    Member

    Please disregard previous post; the code got broken during pasting.

    Code that actually works is posted over at http://wordpress.pastebin.com/zavDfaqZ.

    Posted 11 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.