Forums

[resolved] Page where posts are grouped and then sorted alphabetically (9 posts)

  1. Kahil
    Member
    Posted 2 years ago #

    Ok...so...

    I would like to have an archive page where posts are listed into groups based on their tag and then sorted alphabetically.

    Roughly like...

    A post with tag1
    B post with tag1
    C post with tag1

    A post with tag2
    B post with tag2
    C post with tag2

    Any help would be greatly appreciated.

  2. shawncampbell
    Member
    Posted 2 years ago #

  3. Kahil
    Member
    Posted 2 years ago #

    I'm not looking for a list of links. And that doesn't let you group them by tags, ect.

  4. Kahil
    Member
    Posted 2 years ago #

    I know how to sort them alphabetically using a query that goes in before the loop, per the codex here...I just don't know how to group them by their tag.

  5. Kahil
    Member
    Posted 2 years ago #

    and the only thing in the codex about sorting with tags is to do so with specific tag ids or names.

  6. shawncampbell
    Member
    Posted 2 years ago #

    This is sloppy, and incomplete, and may not even be a proper solution, but it may help you get started:

    <?php $tag = wp_tag_cloud('format=array' ); ?>
    <?php foreach($tag) { ?>
    	<?php wp_tag_cloud(''); ?>
        <?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); ?>
                    <?php if( has_tag($tag) ) { ?>
                        <h3><?php the_title(); ?></h3>
                        <?php the_content('More ยป'); ?>
    				<?php } ?>
                <?php endwhile; ?>
        <?php endif; ?>
    <?php } ?>
  7. Kahil
    Member
    Posted 2 years ago #

    I will try it out and let you know. My other thought was to do something with multiple loops.

  8. Kahil
    Member
    Posted 2 years ago #

    Ok... I have managed to get close by using multiple loops. Now my problem is getting the archive to respect the category I choose. Now it is showing all posts of all categories no matter the one I want to view, while still separating them down by tags.

    any ideas?

  9. Kahil
    Member
    Posted 2 years ago #

    I figured it out on my own. If anyone is interested, here is a sample of the solution.

    At the top of my template, before any loops, I have the following code to check to see what category the page I am on is and return its ID.

    <?php foreach(get_the_category() as $category)
          { $thecat = $category->cat_ID; } ?>

    Then, at the start of each loop I have the following. This code is set to call specific, constant post tags in respect to the particular category archive I am on.

    <h2>30 Sheets</h2>
              <?php query_posts('cat=' . $thecat . '&tag=30-sheets&order=ASC'); ?>
              <?php if (have_posts()) : ?>
              <?php while (have_posts()) : the_post(); ?>
    
              ---- THE CONTENT YOU WANT TO SHOW GOES HERE ----
    
              <?php endwhile; ?>
    
              <?php else : ?>
              <div id="page">
              <p class="center">There are no records for this media.</p>
              </div>
    
              <?php endif; ?>
    
              <h2>Mall Kiosks</h2>
              <?php query_posts('cat=' . $thecat . '&tag=mall-kiosks&order=ASC'); ?>
              <?php if (have_posts()) : ?>
              <?php while (have_posts()) : the_post(); ?>
    
              ---- THE CONTENT YOU WANT TO SHOW GOES HERE ----
    
              <?php endwhile; ?>
    
              <?php else : ?>
              <div id="page">
              <p class="center">There are no records for this media.</p>
              </div>
    
              <?php endif; ?>

    You can do this for as many loops and tags as you like.

Topic Closed

This topic has been closed to new replies.

About this Topic