我的小部件在小部件之前和小部件之后不打印

时间:2017-05-18 作者:Muaz Hesam Musa

我面临着边栏小部件的问题。我已经注册了侧栏并使用了wp默认侧栏,但它既不在其容器div中也不在未打印中before_widgetafter_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>\',
    ));  
}

和索引。php代码在这里

<?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();

和输出页面上的源代码

<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>

我不知道小部件后面的是什么,小部件在外面col-md-4

3 个回复
SO网友:dheeraj Kumar

首先将此代码添加到函数中。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>\',
    ));
将此代码添加到要显示小部件的位置后

       <?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>\';
                    }


        } ?>

SO网友:Porosh Ahammed

你犯了一些错误。使用此选项:

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>\',
    ));  
}

SO网友:Jignesh Patel
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.

结束

相关推荐

“Sidebar”模板中包含评论计数的最近帖子

我试过这个代码,但它完全破坏了我的网站。我想这是因为我在侧栏模板中WP\\u查询的神圣帖子上下文中做错了什么。请一些WP专家更正此代码好吗?<?php $args = array( \'orderby\' => \'date\' ,\'order\' => \'DESC\' ,\'showposts\'=>6 ); $my_query = new WP_Query($args);