Forums

How to display posts by subcategories (8 posts)

  1. jmsnyder23
    Member
    Posted 4 years ago #

    Hi,

    I've searched and searched the forums for an answer to this ... and I should start out by saying I'm not very proficient in PHP.

    I have a book review website that has four main categories. Each category has multiple subcategories. On the category page (using the category template), I would like to list the posts by subcategory.

    Currently, all posts display under the parent. I want them to display like this:

    PARENT CATEGORY

    Subcategory 1
    • Post 1
    • Post 2

    Subcategory 2
    • Post 3
    • Post 4

    etc ...

    Any clue how to do this? I can't figure it out.

    Thanks in advance!

  2. moshu
    Member
    Posted 4 years ago #

    You cannot do that without creating some custom Category_Templates and custom multiple Loops in it.

  3. jmsnyder23
    Member
    Posted 4 years ago #

    Well I have a custom category template, and a custom loop already to call the posts in the category. What I'm not sure of is how to create a second loop that will return the posts by their subcategory.

    Any suggestions?

  4. moshu
    Member
    Posted 4 years ago #

    I have a custom category template, and a custom loop already to call the posts in the category

    Hmmm... There must be something wrong in your setup because you don't need any special custom Loop in a category template! WP knows better what to display there than you do...

  5. jmsnyder23
    Member
    Posted 4 years ago #

    You're not really helping.

  6. anabelle
    Member
    Posted 4 years ago #

    I've found about 5 posts asking the exact same question (including mine) and no one has an answer :(

    Is this really that hard?

  7. Anonymous
    Unregistered
    Posted 3 years ago #

    I wrote this up at my site WordPressIntoCMS.com. I hope this is what you were after.

    Many people on the WordPress.org support forum are asking how to do this.

    Here is the simple solution I came across. It will allow you to display the title of a child category without displaying the parent category.

    Put this code in your template:

    [php]<?php
    $category = get_the_category();
    echo $category[1]->cat_name;
    ?>[/php]

    (Adapted from this code at WordPress.org.)

    The [1] tells it not to pull the parent category which would be [0] but instead to pull the child category which is [1]. If your hierarchy contains more layers simply change the number accordingly.

  8. rumblepup
    Member
    Posted 3 years ago #

    I've just come across this post, and I've been asking the same thing. @cakeychan, there must be some kind of way to do this dynamically, that should be the benefit of a dynamic application.
    @moshu, please have a little more patience with us noobs. We are only asking for help.

Topic Closed

This topic has been closed to new replies.

About this Topic