0

I am facing problems with the sidebar widgets.I have registered sidebar and used wp default sidebars but it is neither within its container div not printing before_widget and after_widget

add_action( 'widgets_init', 'my_sidebar_widget' );
function my_sidebar_widget(){
    register_sidebar( array(
       'name'           => __('Home Page Sidebar', 'nefar'),
       'id'             => 'sidebar-1',
       'before_widget'  => '<div class="sidebar-block">', 
       'before_widget'  => '</div>', 
       'before_title'   => '<h4 class="sidebar-heading">',
       'after_title'    => '</h4>',
    ));  
}

And the index.php codes are here

<?php
get_header();
?>
<div class="container">
    <div class="row">
        <div class="col-md-8">
            <?php
                if(have_posts()){
                    while(have_posts()){
                        the_post();
                        ?>
                            <h3><?php the_title(); ?></h3>
                        <?php
                    }
                }
            ?>
        </div>
        <div class="col-md-4">
            <?php
                dynamic_sidebar('sidebar-1');
            ?>
        </div>
    </div>
</div>
<?php
get_footer();

And on output page source code

<div class="container">
<div class="row">
    <div class="col-md-8">
                                    <h3>The 15 Secrets That You Shouldn&#8217;t Know About Toys.</h3>
                                                <h3>eMusic’s owners believe they can convince users start buying songs again</h3>
                                                <h3>Twitter invokes the allure of one of its biggest users for its billboard ads</h3>
                                                <h3>Spring Asparagus with Creamy Burrata &#038; Pesto</h3>
                                                <h3>Falling Leaves: Endearing Portraits of a Grandmother, a Grandson</h3>
                            </div>
    <div class="col-md-4">
        </div><h4 class="sidebar-heading">Search</h4><form role="search" method="get" id="searchform" class="searchform" action="http://localhost/wordpress/">
            <div>
                <label class="screen-reader-text" for="s">Search for:</label>
                <input type="text" value="" name="s" id="s" />
                <input type="submit" id="searchsubmit" value="Search" />
            </div>
        </form></li>
    </div>
</div>

I Don't know whre the is coming from after the widget and the widget is outside col-md-4

1
  • 2
    In your code no after widgets. Two time before widgets. Please change it and try one again. May 18, 2017 at 3:59

3 Answers 3

1

first add this code to functions.php

register_sidebar(array(
        'name' => __('Top Footer', 'educate'),
        'id' => 'top-footer',
        'description' => __('Top Full width footer.', 'educate'),
        'before_widget' => '<div class="footer-widget %2$s" id="%1$s" >',
        'after_widget' => '</div>',
        'before_title' => '<h3 class="footer-widget-title">',
        'after_title' => '</h3>',
    ));

after add this code where you want to show widget

       <?php if (is_active_sidebar('top-footer')) {

        if (is_active_sidebar('top-footer')) {
                        echo '<aside class="col-md-12 col-sm-6">';
                        dynamic_sidebar('top-footer');
                        echo '</aside>';
                    }


        } ?>
0
add_action( 'widgets_init', 'my_sidebar_widget' );

    function my_sidebar_widget(){
        register_sidebar( array(
           'name'           => __('Home Page Sidebar', 'nefar'),
           'id'             => 'sidebar-1',
           'before_widget'  => '<div class="sidebar-block">', 
           'after_widget'  => '</div>', 
           'before_title'   => '<h4 class="sidebar-heading">',
           'after_title'    => '</h4>',
        ));  
    }

I hope is useful.

0

You make some mistake. Use this:

add_action( 'widgets_init', 'my_sidebar_widget' );
function my_sidebar_widget(){
    register_sidebar( array(
       'name'           => __('Home Page Sidebar', 'nefar'),
       'id'             => 'sidebar-1',
       'before_widget' => '<div id="%1$s" class="widget %2$s sidebar-block">',
    'after_widget'  => '</div>',
    'before_title'  => '<h4 class="sidebar-heading">',
    'after_title'   => '</h4>',
    ));  
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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