Site logo

Archived topic

Using an Element to show parent category posts in a side bar?

6 replies · Started by Bandara on April 19, 2021

Viewing posts 1–7 of 7

Hello! I've been trying to figure this out all day and hope I am just missing something simple.

I have a category of Videos with several child categories of different series.

When displaying a single post categorized as one of these child categories, I would like to have some blocks added to the sidebar that would show recent posts from all of the child categories under Video.

Unfortunately WordPress doesn't seem to consider all of those posts with the child category as also belonging to the parent category. (Except when filtering posts in the dashboard post listing, which is what makes me hopeful I can get this working.) It seems like something so obviously useful but I can't figure out how to do it. I can't figure out how to have the Element target all of those child category posts, and I also can't figure out how to list new posts of all child categories of Video. The second bit isn't a GP issue, but my hope was that I could somehow set this as the default logic across my whole site.

Now, I know that if I just tick the parent category as well as the child, then the problem is solved. However I know my team members are likely to forget to do that. Because why would you?

I also know that I could just list out all the child categories in the display section of the element, etc. But I can't be sure that someone who creates a new child category would remember or even know how to add it in all the right places.

And I also realize that I could probably solve this by creating a CPT for all the videos. If you don't have a solution then that may be the way I have to go. It's not ideal, though, since at times I want to display all posts and I can't always target CPTs.

Ideas? Sorry for the long message. Let me know if it isn't clear.

Someone gave me a link to these suggestions. https://wordpress.stackexchange.com/questions/100707/automatically-assign-parent-terms-when-a-child-term-is-selected

It's not the perfect solution since it just involves having parents ticked, but that may be the best I get.

Let me know if you have any suggestions.

EDIT: Well, I looked at the code more closely (I only have a very, very basic knowledge of php) and it seems like it targets specific categories. I certainly don't expect you good folks to write the code for me. I'm just collecting information.

Hi there,

Not exactly sure If I fully understand but to clarify:

To summarize, you want to place a block and/or a recent post listing of the child category post pages on the sidebar?

If that's the case I believe this should be doable with just the Block Element + WP Show Posts plugin for the Recent post listing.

For the recent post, you can create a Block element containing the WP Show Posts shortcode hooked to generate_before_right_sidebar_content hook. You then set the display rule location dropdown to "Post category - your child category name".

But this can get tedious as you have to repeat this for all the child categories.

We can refine this to use only 1 Block element and 1 WP Show Post list though. But this will require custom PHP coding.

Is the child category the sole category assigned to the mentioned post?

Yes, I was afraid this would end up being a custom php thing.

I certainly don't expect you to do this. I still have some other options to test out. It seems like people have written code to automatically check all parent categories when saving. Although I can't yet get the code working that may be my best bet, short of restructuring things. There is this very old plugin, but it doesn't seem to work https://wordpress.org/plugins/parent-category-toggler/

I think you have understood, but let me try to explain another way. When someone is viewing one of the video posts on my site (that would be categorized as a child to the parent "Video" category), I would like to have a side bar that included the latest videos from all the video sub categories. That way they might discover a new series (aka sub-category).

And as you point out, I was trying to do this without having to make sure the code got updated every time I added a new sub category.

I've figured out a few new terms to google for, so I will keep working on it.

Thanks!

I think you have understood, but let me try to explain another way. When someone is viewing one of the video posts on my site (that would be categorized as a child to the parent “Video” category), I would like to have a side bar that included the latest videos from all the video sub categories. That way they might discover a new series (aka sub-category).

So the recent post will be visible to ALL child/subcategory of "Video" post?

and the recent post query ALL video subcategories, meaning it doesn't have to be specific to what the video post's subcategory is?

If the recent post list for ALL video subcategories will be visible to ALL subcategories of video, I think we don't need PHP.

Here's how I'd do it:
1.) Create a WP Show Post list showing ALL posts from Video subcategories.
2.) Get its shortcode.
3.) Create a Block Element
4.) Add the shortcode through a shortcode block on the Block Element.
5.) Set the block element's hook to generate_before_right_sidebar_content
6.) Set the display rule location to add all the post subcategories.

This way, the WPSP list showing all the video subcategory only shows on single post pages that are under the subcategory we specified on the display rule location.

Thank you! I think that is the method that I will have to follow.

I was hoping to be able to avoid this step:

6.) Set the display rule location to add all the post subcategories.

But it doesn't seem like there is any way to instruct WP to always include posts categorized as children to be included in the parent category.

Thanks!

Avoiding step 6 will require PHP coding. Atleast w/ step 6, you can do it within the UI. :)

No problem, glad to be of any help. :D

This archived topic is closed to new replies.