I am updating my portfolio website and require a different sidebar for different ties of pages, i.e. one for work page and one for the blog page, apparently this can be done using Custom Fields in the page or post.

So, I opened up single.php and found the following code

<?php get_sidebar(); ?>

And replaced it with the code below

<?php $sidebar = get_post_meta($post->ID, "sidebar", true);
get_sidebar($sidebar);
?>

To add a custom sidebar, all I need to do now is apparently add the custom field “Sidebar” and include the name of the sidebar file. For example if I insert “page-01”, it will display sidebar-page-01.php as your sidebar.

After multiple times of trying however this 'isn't' the case, can't see anything wrong with what I am doing, any one have any ideas? Any help would be greatly appreciated!

Thanks guys!

有帮助吗?

解决方案

You can do this the way you are doing it by doing your own php function and target the page you need with

<?php
    if ( is_home() ) :
        get_sidebar( 'home' );
    elseif ( is_portfolio page1() ) :
       get_sidebar( 'portfolio page sidebar' );
    else :
       get_sidebar();
    endif;
?>

add more elseif statements for as many page & sidebar combination you will have

or you can download this plugin and use the code above the same way

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top