Good day, dear coding gurus.

added to the first sidebar that was in the skin, the code of the second (my-sidebar):

function themonic_widgets_init() { register_sidebar( array( 'name' => __( 'Main Sidebar', 'iconic-one' ), 'id' => 'themonic-sidebar', 'description' => __( 'This is a Sitewide sidebar which appears on posts and pages', 'iconic-one' ), 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<p class="widget-title">', 'after_title' => '</p>', ) ); register_sidebar( array( 'name' => __( 'My Sidebar', 'iconic-one' ), 'id' => 'my-sidebar', 'description' => __( 'This is a Sitewide sidebar which appears on posts and pages3', 'iconic-one2' ), 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 'after_widget' => '</aside>', 'before_title' => '<p class="widget-title">', 'after_title' => '</p>', ) ); } add_action( 'widgets_init', 'themonic_widgets_init' ); 

I copied the sidebar.php file and named it sidebar-second.php, made the following changes in it:

 <?php if ( is_active_sidebar( 'my-sidebar' ) ) : ?> <?php dynamic_sidebar( 'my-sidebar' ); ?> некий контент <?php else : ?> некий контент <?php endif; ?> 

And in the file header.php wrote the following structure:

 <?php if ( is_single() ) : ?> <?php $sidebar = get_post_meta($post->ID, "sidebar", true); // левый сайдбар get_sidebar($sidebar);?> <?php else : ?> <?php get_sidebar('second'); ?> <?php endif; // is_single() ?> 

I’m not a programmer, and I can’t understand why, if there is no “arbitrary field” value that a page can specify, it doesn’t pick up sidebar-second.php by default? What needs to be changed in order to pick up and output this sidebar, if a unique one is not connected, which is specified through the value of an arbitrary field?

    1 answer 1

    Because else works on is_single() . That is, the second sidebar will be displayed only when this condition is not met.

    It should be like this:

     <?php if ( is_single() ) { $sidebar = get_post_meta($post->ID, "sidebar", true); // левый сайдбар if ($sidebar) get_sidebar($sidebar); else get_sidebar('second'); } else get_sidebar('second'); // is_single() ?> 
    • Thank you for responding. I copied the code, now the sidebar is displayed only on the pages, but not elsewhere .. In general, the is_single construction is responsible for outputting a certain code on the page with the record and it seems to me that it is not in it at all. Because in the file single.php I entered such code <?php $sidebar = get_post_meta($post->ID, "sidebarg", true); get_sidebar($sidebar); // главный сайдбар ?> <?php $sidebar = get_post_meta($post->ID, "sidebarg", true); get_sidebar($sidebar); // главный сайдбар ?> <?php $sidebar = get_post_meta($post->ID, "sidebarg", true); get_sidebar($sidebar); // главный сайдбар ?> without is_single and other things. And this sitebar is displayed everywhere by default, and if you specify through the "arbitrary field" sidebarg behind the value, it will display the desired sidebar. - UserUser-name
    • Can add value verification to the construction? If it is empty, then output such a sidebar .. if ($ sidebar! = "") ... something like this :-) I wrote something like this, since it is not a programmer :-) - UserUser-name
    • if ($sidebar) just checks that the value is non-empty. - KAGG Design
    • You did not say that you need the output of second on all pages. Corrected the code in the answer. - KAGG Design
    • Thank you very much! It works like this :-) And I didn’t specify, because I don’t understand by what principle the sidebar is selected. Here the main one (right) displays the main one, while the left one (about which conversation) did not deduce anything, such as there is no silence. And how it is set I cannot know, therefore it is necessary, probably, to specify concrete in the code. - UserUser-name