Forums

Show subcategory without hardcoding ID. (4 posts)

  1. weschyrchel
    Member
    Posted 1 year ago #

    I think I posted this in the wrong section before, so I am reposting here. Sorry for the double post!

    Hi guys, I have been working on this for days and any help would be appreciated.

    I am using wordpress as a CMS and I am trying to display the posts from a "grandchild" subcategory. The issue here is I do not want to hardcode any category ID's into the template. I would like one generic single1.php template. I would like it so that we could just add more books in the future by creating a new book category, i.e. "Book C" under the "Books" category and a grandchild category under Book C called "Book C Reviews" where it would display the book in a single1.php template and below the book it would show the reviews for that book.

    I thought I could query_posts, but I can't get to just the "grandchild" category. I appears that I will need a custom function.

    The structure would be like this,

    Books (parent category)
    - Book A (child category)
    -- Book A Review (grandchild category)

    Does anyone have any ideas or can point me in the right direction?

    Thanks, Wes

  2. MAS
    Member
    Posted 1 year ago #

    try once wp_list_category('depth=3')

  3. alchymyth
    The Sweeper
    Posted 1 year ago #

    it is quite complicated as a post can always have more than one category (therefore it will involve some froeach loops, which however will just do what you expect if you only have one top and one child category per post)

    first step:
    get the categories of the post;
    http://codex.wordpress.org/Function_Reference/get_the_category

    in a foreach loop for the above:
    second step:
    isolate the categories of the child category level;
    http://codex.wordpress.org/Function_Reference/get_categories
    use the 'child_of' parameter

    in a foreach loop for the above:
    third step:
    get the children category (i.e. the grandchildren of the top post categories) list of these categories;
    http://codex.wordpress.org/Function_Reference/get_categories
    use the 'child_of' parameter
    (or use the wp_list_categories() in this step:
    http://codex.wordpress.org/Template_Tags/wp_list_categories
    use the 'child_of' parameter )

  4. weschyrchel
    Member
    Posted 1 year ago #

    @ chinmoy, thanks for the feedback, I looked into wp_list_category, but it only gives you the list and not the posts.

    @ alchymyth, thank you for the detailed response I will look into all of those avenues. I knew it was complicated.

    Thanks for the help guys!

Topic Closed

This topic has been closed to new replies.

About this Topic