条件声明

时间:2014-12-17 23:11:54

标签: php wordpress

我正在尝试根据用户所在的页面显示横幅图片:

<?php if ( is_product_category('cocktail-catering-packages') ) { ?>
    <div id="page_caption" class="hasbg parallax " data-image="<?php echo get_bloginfo('template_directory');?>/images/banner08.jpg" data-width="2142" data-height="454">
<?php } ?>
<?php if ( is_product_category('sweet-bites') ) { ?>
    <div id="page_caption" class="hasbg parallax " data-image="<?php echo get_bloginfo('template_directory');?>/images/banner10.jpg" data-width="2142" data-height="454">
<?php } ?>
<?php else { ?>
    <div id="page_caption" class="hasbg parallax " data-image="<?php echo get_bloginfo('template_directory');?>/images/banner04.jpg" data-width="2142" data-height="454">
<?php } ?>

但是,我收到以下错误:

Parse error: syntax error, unexpected 'else' (T_ELSE) in /home/.../public_html/.../wp-content/themes/.../woocommerce.php on line 513

由于我不熟悉PHP,有人可以协助我如何创建此条件语句。

2 个答案:

答案 0 :(得分:2)

只需编写更易读的代码就可以帮助您诊断问题:

function showBanner($filename)
{
  $url = get_bloginfo('template_directory').'/images/'.$filename;
  echo '<div id="page_caption" class="hasbg parallax" data-image="'.$url.
       '" data-width="2142" data-height="454">';
}  

if (is_product_category('cocktail-catering-packages')) 
{
  showBanner('banner08.jpg');
}  
else if (is_product_category('sweet-bites'))
{
  showBanner('banner10.jpg');
}
else 
{ 
  showBanner('banner04.jpg');
}

答案 1 :(得分:0)

这应该适合你:

(只需获取}之前的else

<?php if ( is_product_category('cocktail-catering-packages') ) { ?>
    <div id="page_caption" class="hasbg parallax " data-image="<?php echo get_bloginfo('template_directory');?>/images/banner08.jpg" data-width="2142" data-height="454">

<?php } ?>
<?php if ( is_product_category('sweet-bites') ) { ?>
    <div id="page_caption" class="hasbg parallax " data-image="<?php echo get_bloginfo('template_directory');?>/images/banner10.jpg" data-width="2142" data-height="454">

<?php } else { ?>
    //^Here i have the '}' before the else statement and NOT in separate php tags
    <div id="page_caption" class="hasbg parallax " data-image="<?php echo get_bloginfo('template_directory');?>/images/banner04.jpg" data-width="2142" data-height="454">

<?php } ?>