简体   繁体   中英

Wordpress change sidebar for specific post category

I want to change the sidebar of the category "Magazin". I have tried this code:

<?php  $thecat = single_cat_title( '', false ); ?>
        <?php  if ($thecat == 'Magazin') { dynamic_sidebar('Magazin'); } else { dynamic_sidebar ('sidebar'); }  ?>

But the sidebar "Magazin" does not show in the magazin category posts. However if i just set it just like that:

dynamic_sidebar('Magazin');

It shows the sidebar properly (but in every category).

To show a custom sidebar for different categories in a single post you can use the following code:

$cat = get_the_category();

if ( ! empty( $cat ) ) {
    $cat_name = $cat[0]->name;
    switch ($cat_name) {
        case 'Category I':
            include ('sidebar-cat-i.php');
            break;
        case 'Category II':
            include ('sidebar-cat-ii.php');
            break;
        case 'Category III':
            include ('sidebar-cat-iii.php');
            break;
    }
} else {
  get_sidebar();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM