• I would like to create a custom sidebar so that the sidebar menu only displays a list of post titles from the current parent category (not the post titles from other categories). It would also display a list of all other categories but not the titles of their posts.

    So for example if I’m currently viewing a post from category two it only displays the list of all posts from that current category like the following:

    Category one
    Category two
    – Child category two (post 1)
    – Child of category two (post 2)
    – Child of category two (post 3)
    – Child of category two (post 4)
    – Child of category two (post 5)
    Category three
    Category four
    Category five
    Category six

    If I’m viewing a post from category four it only displays the post titles under that category like so:

    Category one
    Category two
    Category three
    Category four
    – Child category four (post 1)
    – Child of category four (post 2)
    – Child of category four (post 3)
    – Child of category four (post 4)
    – Child of category four (post 5)
    Category five
    Category six

    Which file would I edit and if you could provide the code that would be much appreciated.
    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Casey Driscoll

    (@caseypatrickdriscoll)

    Hey universaltruth,

    Thanks for posting!

    I played around with this a bit, adding some sample ‘fruit’ and ‘vegetable’ categories and posts. There are a number of ways to implement the menu, and the solution will depend on a number of things in your site.

    Ultimately, I would look into the body_class() function, for possibly adding the ‘category’ class to the body tag. There may be some custom coding needed, including php, css and javascript, but you probably just want to ‘hide’ the children of the non-current-category.

    Also, could I get a piece of clarification? Will the ‘children’ in the menu be child categories or child posts? The solution may depend on the implementation.

    Does this help you get started? If you could reply back with a sample link and the name of your theme, I may be able to help you more.

    Hope this helps. Cheers!

    Casey Driscoll

    (@caseypatrickdriscoll)

    Here is an example of the body_class function showing the category of a category page.

    https://codex.wordpress.org/Function_Reference/body_class

    View post on imgur.com

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How do I create a custom sidebar that only lists posts from current category’ is closed to new replies.