开发者

PHP & WP: Render Certain Markup Based on True False Condition

So, I'm working on a site where on the top of certain pages I'd like to display a static graphic and on some pages I would like to display an scrolling banner.

So far I set up the condition as follows:

<?php 
    $regBanner = true;
    $regBannerURL =  get_bloginfo('stylesheet_directory'); //grabbing WP site URL 
?>

and in my markup:

<div id="banner">
  <?php

if ($regBanner) {
  echo "<img src='"  . $regBannerURL . "/style/images/main_site/home_page/mock_banner.jpg' />";
}

else {
  echo 'Slider!';
}

?>
</div><!-- end banner -->

In my else statement, where I'm echoing 'Slider!' I would like to output the markup for my slider:

  <div id="slider">
    <img src="<?php bloginfo('stylesheet_directo开发者_JAVA技巧ry') ?>/style/images/main_site/banners/services_banners/1.jpg" alt="" />
    <img src="<?php bloginfo('stylesheet_directory') ?>/style/images/main_site/banners/services_banners/2.jpg" alt="" />
    <img src="<?php bloginfo('stylesheet_directory') ?>/style/images/main_site/banners/services_banners/3.jpg" alt="" />
        .............
  </div>

My question is how can I throw the div and all those images into my else echo statement? I'm having trouble escaping the quotes and my slider markup isn't rendering.


<div id="banner">
  <?php if($regbanner): ?>
      <img src="<?php echo $regBannerURL; ?>/style/images/main_site/home_page/mock_banner.jpg" />
  <?php else: ?>
    <div id="slider">
      <img src="<?php echo ($bannerDir = bloginfo('stylesheet_directory') . '/style/images/main_site/banners/services_banners'); ?>/1.jpg" alt="" />
      <img src="<?php echo $bannerDir; ?>/2.jpg" alt="" />
      <img src="<?php echo $bannerDir; ?>/3.jpg" alt="" />
          .............
    </div>
  <?php endif; ?>
</div><!-- end banner -->


If you don't like the offered solution using the syntaxif(...):...else...endif; you also have the possibilty of using heredoc-style to include bigger html-parts into an echo-statement without the need of escaping it.

The code-formatting in here unfortunatly messed up my example, which I wanted to post. But if you know the heredoc-notation, it should not be a problem ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜