我在wordpress中有一个定制的侧边栏,这是sidebar.php中的工作代码:
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<aside id="secondary" class="sidebar-area widget-area">
<div class="sidebar-area-wrap">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</div>
</aside><!-- #secondary -->我在functions.php(works)中添加了选项,所以现在代码如下所示:
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
$swag_sidebar_location = get_theme_mod('swag_sidebar_location');
if (isset($swag_sidebar_location) && $swag_sidebar_location=='') {
$swag_sidebar_location ='sidebar-location-none';
}
?>
<?php
if (isset($swag_sidebar_location) && $swag_sidebar_location=='sidebar-location-right' || $swag_sidebar_location=='sidebar-location-left') {
echo '
<aside id="secondary" class="sidebar-area widget-area">
<div class="sidebar-area-wrap">
' . dynamic_sidebar( "sidebar-1" ) . '
</div>
</aside>
';
}
?>问题是,小部件出现在标签外面和标签之前,数字"1“出现在标记中。
我猜问题就在这里:' . dynamic_sidebar( "sidebar-1" ) . ' --我不知道php --只是想找出答案。我怎么才能解决这个问题?
编辑: HTML外观的示例如下:
<section id="pages-2" class="widget">xxxxxx</section>
<section id="pages-2" class="widget">xxxxxx</section>
<section id="pages-2" class="widget">xxxxxx</section>
<section id="pages-2" class="widget">xxxxxx</section>
<aside id="secondary" class="sidebar-area widget-area sidebar-location-right"> <div class="sidebar-area-wrap"> 1 </div> </aside>而不是像这样正确地看上去:
<aside id="secondary" class="sidebar-area widget-area sidebar-location-right">
<div class="sidebar-area-wrap">
<section id="pages-2" class="widget">xxxxxx</section>
<section id="pages-2" class="widget">xxxxxx</section>
<section id="pages-2" class="widget">xxxxxx</section>
<section id="pages-2" class="widget">xxxxxx</section>
</div>
</aside>编辑:根据要求,我在functions.php中的注册侧栏代码
function swagger_widgets_init()
{
register_sidebar(array(
'name' => esc_html__('Sidebar', 'swagger'),
'id' => 'sidebar-1',
'description' => esc_html__('Add widgets here.', 'swagger'),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title"><span class="widget-title-span">',
'after_title' => '</span></h2>'
));
}
add_action('widgets_init', 'swagger_widgets_init');发布于 2019-02-10 23:56:24
好旧的Wordpress和它的“功能回响”。
dynamic_sidebar()不是return值;它是echo值。这使得它不适合在字符串中连接,因为在字符串计算期间,它的结果将是echod,甚至在创建字符串之前将其发送到输出缓冲区。
不幸的是,Wordpress没有提供此函数的get_*版本来返回字符串(请参阅https://core.trac.wordpress.org/ticket/13169)。
而不是连接,而是执行三个单独的调用。例如..。
echo '<aside id="secondary" class="sidebar-area widget-area"><div class="sidebar-area-wrap">';
dynamic_sidebar( "sidebar-1" );
echo '</div></aside>';您也可以使用单个echo语句,但不需要连接,而是传递几个逗号分隔的参数,例如
echo '<aside id="secondary" class="sidebar-area widget-area"><div class="sidebar-area-wrap">',
dynamic_sidebar( "sidebar-1" ),
'</div></aside>';另一个选择是坚持原来的混合HTML和PHP,包装在您的if条件中。例如
if (isset($swag_sidebar_location) && $swag_sidebar_location=='sidebar-location-right' || $swag_sidebar_location=='sidebar-location-left') : ?>
<aside id="secondary" class="sidebar-area widget-area">
<div class="sidebar-area-wrap">
<?php dynamic_sidebar( 'sidebar-1' ) ?>
</div>
</aside>
<?php endif ?>请参阅http://php.net/manual/control-structures.alternative-syntax.php
https://stackoverflow.com/questions/54599479
复制相似问题