duanlisha2335 2012-07-04 12:15
浏览 508

使用PHP根据页面ID显示不同的HTML块

I'm trying to use PHP to output different HTML for an image slider dependent on the body's page id of a wordpress site

I've tried the below, but being a newbie to PHP, i seem to be getting in a bit of a pickle instead. It just needs to be simple - I can do it via CSS however I'd rather not have all the images loading only to display a select few of them. I'd also rather not use a plugin if possible.

Any pointers would be greatly appreciated.

Thank you

<?php if($id == "page-id-19") {
?>
<div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image2.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image3.jpg" alt="" /></div>
<?php
}
elseif ($id == "page-id-23") {
?>
<div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image4.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image5.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image6.jpg" alt="" /></div>
<?php
}
?>

**UPDATE****

I realised that the id was actually a class.

eg. "body class="page page-id-19 page-template page-template-inner-page-php"

However, when using the suggested solution below, it just doesn't doesn't output any HTML at all to the page... it's being called to the template page via an include -

    <?php
   switch($class){
     case "page-id-19":
?>
         <div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image2.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image3.jpg" alt="" /></div>
<?php   
     break;
     case "page-id-23":
?>
         <div class="imageSlider"><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image4.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image5.jpg" alt="" /><img src="<?php echo bloginfo('template_directory');?>/Images/mainImages/innerPage-Image6.jpg" alt="" /></div>
<?php
     break;
   }
?>
  • 写回答

2条回答 默认 最新

  • duanpie2834 2012-07-04 12:24
    关注

    I may have misunderstood the question, but the way I understand what you're asking, I think you could best achieve this using a switch.

    The Switch:

    <?php
       switch($id){
         case "page-id-a":
    ?>
             Put your HTML here.
    <?php   
         break;
         case "page-id-b":
    ?>
             Put your HTML here.
    <?php
         break;
       }
    ?>
    

    Where I put page-id-a or page-id-b , let these be what you currently have as page-id-23 etc.

    It's not as complex as it looks, and it's horrendously logical.

    You can read up more on switches at :

    http://www.w3schools.com/php/php_switch.asp

    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类