• 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

Automatically assign a custom sidebar to a category

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 › Design Tips and Tricks › Automatically assign a custom sidebar to a category

This topic is: not resolved

Tagged: custom sidebar, Sidebar

  • This topic has 2 replies, 2 voices, and was last updated 6 years, 1 month ago by frankie.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • January 25, 2017 at 7:41 am #200000
    frankie
    Member

    Hi
    I wonder if someone is able to check that I have set up this code correctly please as I am not confident working with php and the functions.php file?!
    We have a large site with multiple custom sidebars using Simple Sidebars. But now we need to change a sidebar for one category only but this category has about 300 posts so I don't want to go through changing these manually! The category has a slug of 'here-now-aircraft' and the sidebar we need to allocate is 'hn-aircraft'.

    Based on the code from a previous Post (http://www.studiopress.community/topic/code-to-automatically-assign-a-custom-sidebar-to-posts/) I created the following. Will this work so that it retains all the current sidebars and their Posts, but allocates one specific sidebar to one specific category defined:

    ----------

    add_action( 'genesis_before_sidebar_widget_area', 'themeprefix_remove_sidebar' ); // starts the ball rolling

    function themeprefix_remove_sidebar() {
    if ( is_category( 'here-now-aircraft' ) ) { // set your connditionals here
    remove_action( 'genesis_sidebar', 'ss_do_sidebar' ); // removes Simple Sidebar
    remove_action( 'genesis_sidebar', 'genesis_do_sidebar' ); // removes Genesis Default sidebar
    add_action( 'genesis_sidebar', 'themeprefix_add_sidebar' ); // adds alternative sidebar in function below
    }
    }

    // Alternative Sidebar
    function themeprefix_add_sidebar() {
    dynamic_sidebar( 'hn-aircraft' ); // add in the ID of the sidebar you want to replace it with
    }

    ------

    Many thanks

    http://www.studiopress.community/topic/code-to-automatically-assign-a-custom-sidebar-to-posts/
    January 25, 2017 at 11:43 pm #200039
    Brad Dalton
    Participant

    I assume you have also registered a new widget in functions.php with the i.d hn-aircraft ?

    Also, i suggest you use in_category which enables you to load the sidebar on all single posts within the category rather than is_category which returns true on the category archive page.


    Tutorials for StudioPress Themes.

    January 26, 2017 at 6:54 am #200061
    frankie
    Member

    Great that works perfectly and thanks for the extra suggestion, Brad.

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘Design Tips and Tricks’ 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

© 2023 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