Community Forums › Forums › Archived Forums › General Discussion › How to show only sub categories on a category page in the sidebar?
Tagged: Child Pages, sub pages
- This topic has 14 replies, 3 voices, and was last updated 8 years ago by carasmo.
-
AuthorPosts
-
March 23, 2016 at 11:23 am #182108leighmMember
Hi,
On a page (not post) that has subcategories, I would like to show only that category and it's subcategories in the sidebar. I found this code which might be the solution but I don't know where to put it:
Or does anyone have an other ideas for how to do this?
Thanks,
March 23, 2016 at 8:48 pm #182144Brad DaltonParticipantIf you can't write code, i suggest you spend some time searching for a plugin which includes the functuonality you need built into the plugin.
March 24, 2016 at 2:47 am #182152leighmMemberHi Brad,
I can write some code, but this is the first time I've used Genesis, and as we can't just go and edit the framework, I'm unsure of where we put custom code. And I really want to learn how to do this. This is what I've attempted. I've copied the sidebar.php file from the Genesis framework into my child theme (Enterprise Pro), and added this code:
// I'm hoping that this will execute my function in the correct place? add_action( 'genesis_sidebar', 'enterprise_sidebar_control_categories' ); // should work out what category page I'm on and display only subcategories from that categories? function enterprise_sidebar_control_categories() { if (is_category()) { $this_category = get_category($cat); if (get_category_children($this_category->cat_ID) != "") { echo "<h1>Subcategories</h1>"; echo "<ul>"; wp_list_categories('orderby=id&show_count=0&title_li= &use_desc_for_title=1&child_of='.$this_category->cat_ID); echo "</ul>"; } } }
However, this code did not work for me. Nothing changes. Firstly, am I hooking the function in at the right place? And if so, is the code snippet the correct one to achieve what I'm looking to do.
Any help you can offer would be greatly appreciated? Or if you know of any resources that deal with creating custom templates, that would be great.
Thanks,
Leigh
March 24, 2016 at 4:13 am #182156Brad DaltonParticipantThat's exactly how you do it in genesis, with hooks, however the code from Yoast may need updating based on my testing as it didn't work for me.
I would look for a plugin which includes this functionailty to see how its coded and use that as a guide or find another snippet.
March 24, 2016 at 7:24 am #182164carasmoParticipantuse a priority of at least 0 on your add_action, nothing shows up otherwise.
function test_sidebar_hook() { echo '<h1 style="background:green">test sidebar hook</h1>'; } add_action('genesis_sidebar', 'test_sidebar_hook', 0);
March 24, 2016 at 11:34 am #182172leighmMemberThanks Carasmo. I tried adding your test code to my custom sidebar, but nothing shows up at all? It does work if I add it to functions.php
Also, I noticed that when my custom file is active, the page shows the blog post twice?
Brad, I found this updated code on: http://www.wpbeginner.com/wp-tutorials/display-subcategories-on-category-pages-in-wordpress/. So, my function looks like this:
function enterprise_sidebar_control_categories() { if (is_category()) { $this_category = get_category($cat); } if($this_category->category_parent) { $this_category = wp_list_categories('orderby=id&show_count=0 &title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent. "&echo=0"); } else { $this_category = wp_list_categories('orderby=id&show_count=0 &title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID. "&echo=0"); } if ($this_category) { echo "<ul>"; echo $this_category; echo "</ul>"; } }
But it still doesn't work.
If I add it to functions.php, it shows all the post categories and subcategories. So, I don't think the code is doing what it should. Specifically, I'd like this to work on pages not posts, and show the subcategories (and the parent) of any page the user is on.
I've had a look at some plugins but can't find one that does this. Any recommendations?
Thanks,
Leigh
March 24, 2016 at 8:29 pm #182195carasmoParticipantMost everything about Genesis, except for a few situations, is done in the functions.php file. Pages are not hierarchical, there are no categories for pages. You can show the children of pages -- there's a tut around the web, one by Bill Erickson.
This one (below) works for posts, but you modify as needed to work with Genesis (no php tags and hook it with the priority).
http://wordpress.stackexchange.com/a/145916/64742
You'll need the global $post variable since you are outside the loop.
March 25, 2016 at 3:06 am #182207Brad DaltonParticipantI spent a few hours working this out https://www.youtube.com/watch?v=r32ZFg1JbwA
March 25, 2016 at 5:51 am #182214carasmoParticipantOhhh. An archive page with children. I was thinking post type page when OP mentioned pages. I need a translator.
March 25, 2016 at 9:47 am #182233leighmMember@carasmo Ok, I'm confused, so are you saying that I don't need to create a custom sidebar template, and I should put this code into the functions file? I had done this earlier and Brad had indicated that it was the right way to do it?
@braddalton Thanks for that video Brad. Although, that deals with blog posts. I'm trying to display subcategories of pages. Carasmo has indicated that there are no subcategories for pages, but when I add a page I'm able to assign it to a parent. Is that not the same as a subcategory? The video points to your wpsites.net but I have to pay to get access to it. Does being a member of StudioPress not give me access to this code?Thanks.
March 25, 2016 at 10:28 am #182238carasmoParticipantOkay.
I don't understand what you want. You have pages, a post type page, which is set up by clicking "Pages" in the WP Admin. Post Type pages are not hierarchical, they have no categories at all. They can have child pages, you can use the quick editor or the main editor and under the Page Attributes you can choose a parent page. Those are not categories.
An archive shows loop of the content for a tag, for a blog (the main page for a blog is called home inside WordPress and it is an archive too), for a category, for an author... That's what Brad, I believe, thought you were talking about, showing the child categories on a parent category on an archive page.
When I began with WP and Genesis I went through any and all docs to learn the terms, it's in the codex and in videos all over the place. That saved me a lot of time so I could find what I wanted.
You can show children of pages on a page's parent page. https://wordpress.org/plugins/child-pages-shortcode/. There are also other ways of doing this, you can look in the plugin files to see how they did it, you can search with the correct terms "Display child pages WordPress" w/o quotes and so on.
March 25, 2016 at 10:18 pm #182256Brad DaltonParticipantAs it stands, your question is not clear.
Do you want to display sub pages of the parent page in the sidebar?
March 25, 2016 at 10:24 pm #182258Brad DaltonParticipantHere's a plugin for that, one of many you can find on the WordPress plugins site https://wordpress.org/plugins/be-subpages-widget/
March 26, 2016 at 2:58 am #182266leighmMemberThanks a lot for your patience with my question. Obviously, I had the terminology wrong. I was thinking of pages as categories and their child pages as subcategories (which seemed logical enough to me) but I can see now that those terms relate exclusively to posts.
That plugin (https://wordpress.org/plugins/be-subpages-widget/) is exactly what I was looking for. I did a lot of searching but never found it. Again, probably using the wrong search terms.
Thanks again.
Leigh
March 26, 2016 at 6:58 am #182274carasmoParticipantYes, thank you I thought I saved the link but couldn't find Bill's version, which is much better to work with.
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.