• Resolved rwwood

    (@rwwood)


    I’m setting up a site which is comprised of a couple of different “post types”. One is the normal blog entry type short article, but the other is in the form of short multi-chapter booklets. The way that I’ve set up the site so far is with a category structure as follows:

    Articles

    • Article 1
    • Article 2
    • Article 3, etc.

    Booklets
    Booklet 1

    • Chapter 1
    • Chapter 2, etc.

    Booklet 2

    • Chapter 1, etc.

    What I’m trying to accomplish is that, when the Booklet category is loaded, there would be a list of booklet titles rather than a list of the various chapters and their teaser text. I would then like to have just a list of Chapter titles when individual Booklet categories are loaded.

    I’m not great at writing original php code, but have searched the forum and not found a way to do this. Any help would be appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)
  • This template is close to what you want, except it does not handle sub-categories:

    <?php
    /*
    Template Name: listpostsbycategory
    */
    ?>
    <?php /*
     Author: Mac McDonald
     Contact at BluegrassMiataClub.com using About->Contact Us form.
    
     This program creates a list of posts by categories as links to the posts.
    */?>
    <?php get_header(); ?>
       <div id="content" class="narrowcolumn" role="main">
    
    <?php
    foreach (array('Articles','Booklet 1','Booklet 2') as $catname) {
       $catid = get_cat_id($catname);
       $myquery = new WP_Query("cat=$catid");
       if ($myquery->have_posts()) :
          echo "<h2>Posts for Category $catname</h2>";
          while ($myquery->have_posts()) : $myquery->the_post(); ?>
             <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a></p>
          <?php endwhile;
       endif;
    }
    
    ?>
    </div><!-- End content -->
    <?php get_footer(); ?>

    OOPS – the example above was listing thumbnails for each post. To list titles, change ‘the_post_thumbnail()’ to ‘the_title()’.

    Thread Starter rwwood

    (@rwwood)

    The problem that I have is getting any link for the “Booklets” category in the Sidebar dropdown list due to the fact that “Booklets” has no actual posts, but is just a parent to the booklets themselves. Apparently empty categories don’t get listed, even if they’re parents to categories that do contain posts.

    I’ve tried setting the hide_empty argument for wp_list_categories to “0” (false), but that hasn’t seemed to change anything.

    I guess, after researching and doing more reading on the forums and in the docs, that I’m looking for a listing of sub-category names, not the posts that are in those sub-categories so that a reader could drill down from “Booklets” to “Booklet 1” to “Chapter 1”.

    Thanks.

    As far as I can tell, wp_list_categories is ignoring hide_empty. You might be able to roll your own version with get_terms().

    Thread Starter rwwood

    (@rwwood)

    I still don’t think that I’ve been able to communicate what I’m looking for. I’m not looking for a list of POSTS, rather a list of CATEGORY NAMES, whether they contain posts or not, and only direct children of the parent category, without grandchildren, or at least with grandchildren displayed below the corresponding parent, similar to the way I have it outlined in the first post in this thread.

    I know I could tweak the category template to display just titles, but they are all mixed irrespective of what parent they have and without any break in the list to indicate what parent they have, and that’s ok if a post can be categorized, for example, as a Ford (parent category) and a Mustang (child category) and a 1968 Mustang (grandchild category), but doesn’t work for the Booklets/Booklet Title/Chapter Title scenario that I’m trying to accomplish.

    Thanks.

    No, I think you were perfectly clear. I also think that get_terms will do what you want. Give the code below a try to see if it is close:

    <?php
    $catarray = get_terms('category','hide_empty=0&hierarchical=1');
    foreach ($catarray as $category) {
      $id = $category->term_id;
      if ($category->parent == 0) {
        $catbyid[$id]['depth'] = 0;
      } else {
        $catbyid[$id]['depth'] = $catbyid[$category->parent]['depth'] + 1;
      }
    }
    $in_list = 0;
    $top_category = '';
    foreach ($catarray as $category) {
      $id = $category->term_id;
      $name = $category->name;
      $link = get_category_link($id);
      if ($category->parent == 0) {
        // This is a new parent
        if ($in_list) echo '</ul>';  // End the previous list
        $in_list = 0;
        $top_category = $category;
        echo "<h2>$name</h2>";
      } elseif ($top_category->name == 'Booklets' && $catbyid[$id]['depth'] == 1) {
        if ($in_list) echo '</ul>';  // End the previous list
        $in_list = 0;
        echo "<h3>$name</h3>";
      } else {
        if (!$in_list) echo '<ul>';
        $in_list = 1;
        echo "<li><a href='$link'>$name</a></li>";
      }
    }
    if ($in_list) echo '</ul>';
    ?>
    Thread Starter rwwood

    (@rwwood)

    Thanks. I’ll give that a try, but I think I finally found what I want by using a category specific template and wp_list_categories. That gives me a listing of Booklet Titles as links, which can then take the user to the booklet itself.

    Thanks again.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Listing of category names’ is closed to new replies.