• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

How to show only sub categories on a category page in the sidebar?

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › General Discussion › How to show only sub categories on a category page in the sidebar?

This topic is: not resolved

Tagged: Child Pages, sub pages

  • This topic has 14 replies, 3 voices, and was last updated 6 years, 1 month ago by carasmo.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • March 23, 2016 at 11:23 am #182108
    leighm
    Member

    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:

    Showing subcategories on WordPress category pages

    Or does anyone have an other ideas for how to do this?

    Thanks,

    March 23, 2016 at 8:48 pm #182144
    Brad Dalton
    Participant

    If 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.


    2700 Genesis Tutorials

    March 24, 2016 at 2:47 am #182152
    leighm
    Member

    Hi 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 #182156
    Brad Dalton
    Participant

    That'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.


    2700 Genesis Tutorials

    March 24, 2016 at 7:24 am #182164
    carasmo
    Participant

    use 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);
    

    Genesis Theme Customization and Help

    March 24, 2016 at 11:34 am #182172
    leighm
    Member

    Thanks 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 #182195
    carasmo
    Participant

    Most 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.


    Genesis Theme Customization and Help

    March 25, 2016 at 3:06 am #182207
    Brad Dalton
    Participant

    I spent a few hours working this out https://www.youtube.com/watch?v=r32ZFg1JbwA


    2700 Genesis Tutorials

    March 25, 2016 at 5:51 am #182214
    carasmo
    Participant

    Ohhh. An archive page with children. I was thinking post type page when OP mentioned pages. I need a translator.


    Genesis Theme Customization and Help

    March 25, 2016 at 9:47 am #182233
    leighm
    Member

    @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 #182238
    carasmo
    Participant

    Okay.

    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.


    Genesis Theme Customization and Help

    March 25, 2016 at 10:18 pm #182256
    Brad Dalton
    Participant

    As it stands, your question is not clear.

    Do you want to display sub pages of the parent page in the sidebar?


    2700 Genesis Tutorials

    March 25, 2016 at 10:24 pm #182258
    Brad Dalton
    Participant

    Here's a plugin for that, one of many you can find on the WordPress plugins site https://wordpress.org/plugins/be-subpages-widget/


    2700 Genesis Tutorials

    March 26, 2016 at 2:58 am #182266
    leighm
    Member

    Thanks 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 #182274
    carasmo
    Participant

    Yes, thank you I thought I saved the link but couldn't find Bill's version, which is much better to work with.


    Genesis Theme Customization and Help

  • Author
    Posts
Viewing 15 posts - 1 through 15 (of 15 total)
  • The forum ‘General Discussion’ is closed to new topics and replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2022 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble