Using PHP to show different chunks of HTML dependent on page id

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.

Read More

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;
   }
?>

Related posts

Leave a Reply

2 comments

  1. 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