• Hello it’s possible to make un modele sidebar for a model category and child category.

    if I creat a new child category take automatically takes the parent modele sidebar.

    il like the child use its parent sidebar automatically.

    Thank you to helpe me

Viewing 7 replies - 1 through 7 (of 7 total)
  • In your category.php file, try replacing this:

    <?php get_sidebar(); ?>

    With this conditional check:

    <?php // Check category and parent category for matching sidebar
    $cat_id          = get_query_var('cat'); // Get ID of current category
    $cat_term        = get_term_by( 'id', $cat_id, 'category' ); // Use ID to get category data
    $cat_slug        = $cat_term->slug; // Get category slug from data
    $cat_parent_id   = $cat_term->parent; // Get parent category ID from data
    $cat_parent_term = get_term_by( 'id', $cat_parent_id, 'category' ); // Use ID to get parent category data
    $cat_parent_slug = $cat_parent_term->slug; // Get parent category slug from data
    if ( is_active_sidebar( $cat_slug ) ) { // Check for active widgets in sidebar that matches category slug
    	get_sidebar( $cat_slug ); // Load sidebar file that matches category slug
    } else if ( is_active_sidebar( $cat_parent_slug ) ) { // Check for active widgets in sidebar that matches category parent slug
    	get_sidebar( $cat_parent_slug ); // Load sidebar file that matches category parent slug
    } else { // Default if no sidebars match
    	get_sidebar(); // Load default sidebar file
    } ?>

    That code adds a lot of variables to get the category and parent category slugs and is a bit complicated, so I added some comments so that you could understand how it is working better.

    Basically it will check for sidebar with an ID that matches the category slug, if it finds one and that sidebar has widgets, it loads a sidebar file with that category slug. If there isn’t a match, it checks the category’s parent for a sidebar and attempts to load that. If all else fails, it loads the default sidebar.

    This code assumes that the category slugs, sidebar IDs, and sidebar file names all match, like this:

    • Category Slug: category-1
    • Sidebar ID: ‘id’ => ‘category-1’,
    • Sidebar File: sidebar-category-1.php

    I hope that helps!

    Thread Starter Medesko

    (@medesko)

    thank you so much for your answers

    No problem, let me know if that doesn’t do the trick!

    Thread Starter Medesko

    (@medesko)

    Is that it is possible to do by default category Archive.php
    I have a category that are not slug.

    thank you for your understanding

    Thread Starter Medesko

    (@medesko)

    My default category genere by archive.php is
    commerce, sorties, habitat, loisirs

    I created in my thèmes sidebar-commerce.php , sidebar-sorties.php
    sidebar-habitat.php sidebar-losirs.php

    I insert the conditional in artchive.php
    on its ligne for condition I add my default
    get_sidebar(‘right’); // Load default sidebar file

    Now how to make it work with categories that are not slug ?

    This code is intended to go in category.php, it might not work consistently in archive.php. If you don’t have a category.php file, try copying your index.php file and renaming it to make one.

    I’m not sure what you mean by categories that are not slugs, all categories have slugs, regardless of how your permalinks are setup.

    Thread Starter Medesko

    (@medesko)

    Hello It work now in Archives.php
    I juste change your condition Thank u so much
    the code is

    <?php // Check category and parent category for matching sidebar
    $cat_id          = get_query_var('cat'); // Get ID of current category
    $cat_term        = get_term_by( 'id', $cat_id, 'category' ); // Use ID to get category data
    $cat_slug        = $cat_term->slug; // Get category slug from data
    $cat_parent_id   = $cat_term->parent; // Get parent category ID from data
    $cat_parent_term = get_term_by( 'id', $cat_parent_id, 'category' ); // Use ID to get parent category data
    $cat_parent_slug = $cat_parent_term->slug;
    
     // Get parent category slug from data
    
    	if(!theme_dynamic_sidebar( $cat_slug ) AND !theme_dynamic_sidebar( $cat_parent_slug )) {
    		theme_dynamic_sidebar('right');
    	} // Load sidebar file that matches category parent slug
    	//theme_dynamic_sidebar('right'); // Load default sidebar file
    ?>
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Sidebar to Modele category and child’ is closed to new replies.