我正在运行一个wordpress网站,我试图根据不同的类别回声出不同的侧边栏,但我似乎不能把php代码内的回声标签。代码工作得很好,在侧边栏中,在不同的类别页面上,但我只是不能在echo ' '内添加php代码,因为它返回一个错误。这是我的代码:
<?php if ( is_active_sidebar( "main_sidebar" ) ) : ?>
<div id="main_sidebar" class="widget-area">
<?php
if ( is_category( '7' )) {
echo 'Sidebar to be echo'd on category page with ID=7';
}
else
{
echo 'Sidebar to be echo'd on all the other category pages';
}
?>
</div><!-- #first_sidebar .widget-area -->
<?php endif; ?>
,下面是我希望它的功能:
<?php if ( is_active_sidebar( "main_sidebar" ) ) : ?>
<div id="main_sidebar" class="widget-area">
<?php
if ( is_category( '7' )) {
echo '<?php dynamic_sidebar( 'category_fashion' ); ?>';
}
else
{
echo '<?php dynamic_sidebar( 'main_sidebar' ); ?>';
}
?>
</div><!-- #first_sidebar .widget-area -->
<?php endif; ?>
当使用上面的代码时,我得到这个错误:
解析错误:语法错误,期望','或';'在/home/ignoremu/public_html/wp-content/主题/…/sidebar.php第6行
请帮帮我。谢谢。
最好的方法是完全跳过echo中的HTML,只使用echo输出结果:
<?php if ( is_active_sidebar( "main_sidebar" ) ) : ?>
<div id="main_sidebar" class="widget-area">
<?php
if ( is_category( '7' )) {
echo dynamic_sidebar( 'category_fashion' );
}
else
{
echo dynamic_sidebar( 'main_sidebar' );
}
?>
</div><!-- #first_sidebar .widget-area -->
<?php endif; ?>
如果你想在带有单引号的echo中放置单引号,你必须转义引号:
// Escaped here
echo 'Sidebar to be echo''d on category page with ID=7';
在双引号字符串中使用双引号也是一样的。
编辑:哎呀,没看完。修改:
if ( is_category( '7' )) {
echo '<?php dynamic_sidebar( 'category_fashion' ); ?>';
} else {
echo '<?php dynamic_sidebar( 'main_sidebar' ); ?>';
}
:
if ( is_category( '7' )) {
echo dynamic_sidebar( 'category_fashion' );
} else {
echo dynamic_sidebar( 'main_sidebar' );
}
你已经在php中了,为什么要重新启动php ?下面就可以了。
echo dynamic_sidebar( 'category_fashion' );
如果dynamic_sidebar()是一个函数,为什么需要回调它,而不是在函数内部返回/回调值,所以调用它,将返回给定参数的值