下午早些时候,我曾要求解决如何在Wordpress的不同类别页面上回应不同侧边栏的问题。现在我面临的问题是所有类别页面的侧栏下都会显示连续的“1”。但是,当我切换回在所有类别页面上仅显示一个侧边栏的旧代码时,不会发生这种情况。这是最初在sidebar.php文件中的代码,它在所有页面上只回显一个侧边栏:
<?php if ( is_active_sidebar( 'main_sidebar' ) ) : ?>
<div id="main_sidebar" class="widget-area">
<?php dynamic_sidebar( 'main_sidebar' ); ?>
</div><!-- #first_sidebar .widget-area -->
<?php endif; ?>
这是我在sidebar.php文件中放置的修改后的代码,该文件回显了不同类别页面上的不同侧边栏:
<?php if ( is_active_sidebar( "main_sidebar" ) ) : ?>
<div id="main_sidebar" class="widget-area">
<?php
if ( is_category( '7' )) {
echo dynamic_sidebar( 'category_fashion' );
}
elseif ( is_category( '1' )) {
echo dynamic_sidebar( 'category_music' );
}
elseif ( is_category( '5' )) {
echo dynamic_sidebar( 'category_tastemakers' );
}
elseif ( is_category( '11' )) {
echo dynamic_sidebar( 'category_film' );
}
elseif ( is_category( '9' )) {
echo dynamic_sidebar( 'category_model' );
}
elseif ( is_category( '6' )) {
echo dynamic_sidebar( 'category_sports' );
}
elseif ( is_category( '8' )) {
echo dynamic_sidebar( 'category_round' );
}
elseif ( is_category( '10' )) {
echo dynamic_sidebar( 'category_cool' );
}
else
{
echo dynamic_sidebar( 'main_sidebar' );
}
?>
</div><!-- #first_sidebar .widget-area -->
<?php endif; ?>
当我使用原始代码时,侧边栏下方没有显示数字“1”,但当我切换到我修改过的代码时,所有页面的边栏下面都会出现数字“1” 。转到此链接可以看到此行为:http://ignoremusic.com/category/sports/
我还检查了archive.php文件以及functions.php文件,但似乎没有错。这是我在functions.php文件中注册侧边栏的代码部分:
/** SIDEBARS ******************************************************************/
if ( function_exists('register_sidebar') ) {
register_sidebar(array(
'name'=>'sidebar home left',
'id' => 'homeleft_sidebar',
'description' => __( 'sidebar on homepage, left', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'Category: Tastemakers',
'id' => 'category_tastemakers',
'description' => __( 'sidebar on tastemakers category', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'Category: Film',
'id' => 'category_film',
'description' => __( 'sidebar on film category', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'Category: Fashion',
'id' => 'category_fashion',
'description' => __( 'sidebar on fashion category', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'Category: Model Citizens',
'id' => 'category_model',
'description' => __( 'sidebar on model citizens category', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'Category: Sports',
'id' => 'category_sports',
'description' => __( 'sidebar on sports category', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'Category: Round Table',
'id' => 'category_round',
'description' => __( 'sidebar on round table category', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'Category: Cool Stuff',
'id' => 'category_cool',
'description' => __( 'sidebar on cool stuff category', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'Category: Music',
'id' => 'category_music',
'description' => __( 'sidebar on music category', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'sidebar home right',
'id' => 'homeright_sidebar',
'description' => __( 'sidebar on homepage, right', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'main sidebar',
'id' => 'main_sidebar',
'description' => __( 'main sidebar', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'tour dates sidebar',
'id' => 'tour_sidebar',
'description' => __( 'sidebar on tour dates page', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'contact sidebar',
'id' => 'contact_sidebar',
'description' => __( 'sidebar on contact page', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'sidebar footer 1',
'id' => 'sidebarf1',
'description' => __( 'first sidebar in footer', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'sidebar footer 2',
'id' => 'sidebarf2',
'description' => __( 'second sidebar in footer', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'sidebar footer 3',
'id' => 'sidebarf3',
'description' => __( 'third sidebar in footer', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
register_sidebar(array(
'name'=>'sidebar footer 4',
'id' => 'sidebarf4',
'description' => __( 'fourth sidebar in footer', 'gxg_textdomain' ),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widgettitle">',
'after_title' => '</h3>', ));
}
这是archives.php文件中的代码,它反映了侧边栏:
<div id="sidebar" class="grid_3">
<?php get_sidebar(); ?>
</div><!-- #sidebar-->
我尝试在互联网上搜索问题,但找不到任何内容,所以我期待收到你们的回复。谢谢。
答案 0 :(得分:20)
根据https://codex.wordpress.org/Function_Reference/dynamic_sidebar,它应该是
dynamic_sidebar( 'category_fashion' );
而不是 echo dynamic_sidebar( 'category_fashion' );
该函数可能返回true; echo接受一个字符串; true-&gt; string = 1,因此附加1
。
答案 1 :(得分:2)
多余的echo
,由混乱引起,由愚蠢的Wordpress函数引起,这些函数可以打印而不是返回它们。
在您的情况下,以下是错误的:
<?php echo dynamic_sidebar( 'category_fashion' ); ?>
与您之前的正确用法形成对比:
<?php dynamic_sidebar( 'main_sidebar' ); ?>