Support » How-To and Troubleshooting » How to list categories and posts on one page

How to list categories and posts on one page

  • Hi,

    I have an FAQ category on my blog. It has several sub-categories and in each of those several posts. How do I create a loop that will output like this:

    Sub-Category 1
    Question 1
    Question 2
    Sub-Category 2
    Question 3
    Sub-Category 3
    Question 4

    Where each “Question” is a link to the actual blog post.

    And how would I do this dynamically? So that I can have one “category.php” file that will take any category (not just the FAQ one) and display a page like that?


Viewing 7 replies - 1 through 7 (of 7 total)
  • Thanks, but I’m developing a theme and would like to incorporate this feature as a standard.

    Is this really that hard to program using a WP_Query or The Loop (in some way)?

    If I click a category (ie, ?cat=3), how do I “get” that var? Is the only way to get it through $_GET? And after that, constructing a query that gives me all subcategories of that one, and for each of those, give me all posts.

    Does anyone have any ideas about doing that?


    The request is ?cat=2, and this code does the trick.

    $cat_shown = array();
    foreach ($category_cache as $cat_array) {
    foreach ($cat_array as $the_category) {
    $the_ID = $the_category->cat_ID;
    if (in_array($the_ID, $cat_shown))
    $cat_shown[] = $the_ID;
    echo $the_category->cat_name;
    echo "<br>\n";

    And use $posts to get all posts from cat=2 and sub-categories.


    This is exactly what I want to do. Could you explain how this is implemented? Thanks.

    You can also use the get_posts template tag to list the posts in a particular category. I found the implementation of this tag to be very easy. http://codex.wordpress.org/Template_Tags/get_posts

    I would love to have that option – but I don’t get how I should include the code provided by slidese (Mike). Can somebody please help?!

    For what it is worth, I was looking for a way to cycle through categories and list all posts on one page in WordPress MU. I tried Alex King’s Articles Plugin but couldn’t get it to display anything. I ended up combining the post and category queries in a page template. This was pretty simple and works for what I’ve needed so far– I also like that gives me flexibility in what is presented and how it is styled.

    I started with the category query using parameters and styling to show empty categories, exclude Uncategorized, and show the category names styled as headers:

      $categories=  get_categories('hide_empty=0&exclude=1');
      foreach ($categories as $cat) {
      $catname = $cat->cat_name;
      $catid = $cat->cat_ID;
      echo '<h2>'.$catname.'</h2>';

    Then I started the post query calling $catid into the parameters and ordering posts alphabetically:

     $posts = query_posts($query_string . 'cat='.$catid.'&orderby=title&order=asc&posts_per_page=-1'); 
     global $more;
    // set $more to 0 in order to only get the first part of the post
    $more = 0; 
    // the Loop starts here
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    After that I formatted the_title and the_content to suit my needs and followed up with

    <?php endwhile; else:
    _e('There are no posts in this category yet.');
      } //end category query

    Not sure that is the best way to do something like this, but I’m adding it here for what it is worth.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to list categories and posts on one page’ is closed to new replies.