• I got this code, <?php wp_get_archives(‘type=alpha’); ?>, from the wp_get_archives codex page, and it works 90% perfectly for what I need. I’ve been through a ton of pages trying to figure this out, but I’d like it to appear with the category listed.

    The site is basically a dictionary – https://www.thebabydictionary.com, and you can see the page I’m working with at https://thebabydictionary.com/all-letters

    I want it to show the category (such as “A”), then all the posts under it alphabetically.

    There are 2 ways we can skin this cat. A single code that does it all, or we can do 26 individual “get archives” codes, where I can specify each category to go and get. Like I type “A”, and then “get all posts for category 6”, and “B”, “get all posts for category 7”, etc.

    Simpler the better.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Well this will list all categories in alpha order and then the posts under each category:

    <?php
    //get all terms (e.g. categories or post tags), then display all posts in each term
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__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 zebradaddy

    (@zebradaddy)

    That works, though I have some coding cleanup to do.

    What I’d rather explore is pulling the post titles separately for each letter. Then I could type in the category (A, B, C, etc.) and then just pull the post titles underneath it, if I can use the same code 26 times in the template.

    I’m using this in my page-sitemap.php file here: https://thebabydictionary.com/all-letters

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Creating archives by category’ is closed to new replies.