Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing PHP to show different chunks of HTML dependent on page id
    primarykey
    data
    text
    <p>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</p> <p>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.</p> <p>Any pointers would be greatly appreciated.</p> <p>Thank you</p> <pre><code>&lt;?php if($id == "page-id-19") { ?&gt; &lt;div class="imageSlider"&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image.jpg" alt="" /&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image2.jpg" alt="" /&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image3.jpg" alt="" /&gt;&lt;/div&gt; &lt;?php } elseif ($id == "page-id-23") { ?&gt; &lt;div class="imageSlider"&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image4.jpg" alt="" /&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image5.jpg" alt="" /&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image6.jpg" alt="" /&gt;&lt;/div&gt; &lt;?php } ?&gt; </code></pre> <p><em><strong></em>**UPDATE*<em>*</em>**</strong></p> <p>I realised that the id was actually a class.</p> <p>eg. "body class="page page-id-19 page-template page-template-inner-page-php"</p> <p>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 - </p> <pre><code> &lt;?php switch($class){ case "page-id-19": ?&gt; &lt;div class="imageSlider"&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image.jpg" alt="" /&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image2.jpg" alt="" /&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image3.jpg" alt="" /&gt;&lt;/div&gt; &lt;?php break; case "page-id-23": ?&gt; &lt;div class="imageSlider"&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image4.jpg" alt="" /&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image5.jpg" alt="" /&gt;&lt;img src="&lt;?php echo bloginfo('template_directory');?&gt;/Images/mainImages/innerPage-Image6.jpg" alt="" /&gt;&lt;/div&gt; &lt;?php break; } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload