Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to get a sidebar ( get_sidebar( 'mySidebar' ) ) at the top of the page, so I can't call it after get_header() in the content template. How could I do it?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

This method works from your child themes functions.php file.

Change the hook to a different position:

//functions.php
function wpsites_register_widget() {

 register_sidebar( array(
'name' => 'After Header Widget',
'id' => 'after-header',
'before_widget' => '<div>',
'after_widget' => '</div>',
) );
}

    add_action( 'widgets_init', 'wpsites_register_widget' );

    add_filter( 'loop_start', 'after_header_widget', 25 );

    function after_header_widget() {


    if ( is_active_sidebar( 'after-header' ) ) { 
    dynamic_sidebar('after-header', array(
    'before' => '<div class="after-header">',
    'after' => '</div>',
) );


    }

}

Or you can call the sidebar directly in your template file.

<?php if ( is_active_sidebar( 'after-header' ) ) : ?>
<ul id="after-header">
<?php dynamic_sidebar( 'after-header' ); ?>
</ul>
<?php endif; ?>
share|improve this answer
    
I've tried, but the sidebar is called after the navigation bar. I want to call it before. –  Manolo Jan 13 at 10:51
    
Use the template code before the nav –  Brad Dalton Jan 13 at 10:55
    
Well, I thought that overriding header.php file wasn't a recommended method. –  Manolo Jan 13 at 12:41
    
It is fine to create a header.php in your child theme. It might not be ideal and you should review your header.php to see if there are any hooks you might be able to add after_header_start() to. Impossible to say without seeing your header.php file. –  helgatheviking Jan 13 at 12:57

Yes you can, call it after get_header().
Make sure sidebar-{name}.php i.e sidebar-mySidebar.php file is present.
Otherwise let me know the error.

share|improve this answer
    
The problem is that I want to call it before all the elements in the page, so calling it after header will put the sidebar content below the "nav-bar". –  Manolo Jan 13 at 9:59
    
then you can copy header.php file and call sidebar before navigation menu. –  shahpranaf Jan 13 at 10:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.