I would like to add a tag around the intro text, but only if the article has an intro image.

It's easy to add markup around the image in the intro_image layout file. And in the file blog_item.php, I can see where the text is included:

<?php echo $this->item->introtext; ?>

But can anyone help me write an if statement so that I can add a <div> around the text so that I can control the layout if the intro image has been set?

  • Sorry, that bit of php doesn't show up right in the post - I don't know how to add it correctly but I hope you know what I mean. – LeilaH Jun 8 '18 at 14:43

If you mean the intro image that's used in intro_image layout, use the same check as in the layout:

<?php $images = json_decode($this->item->images); ?>
<?php if (isset($images->image_intro) && !empty($images->image_intro)) : ?>
    <div><?php echo $this->item->introtext; ?></div>
<?php else : ?>
    <?php echo $this->item->introtext; ?>
<?php endif; ?>
  • No, I want to put a div around the text, but ONLY if there is an image. If there's no intro image, no div around the text. Basically, I want a column effect if there is an image. – LeilaH Jun 28 '18 at 11:17
  • Yes, that's what this snippet does. Replace <?php echo $this->item->introtext; ?> line with it. – Sharky Jun 29 '18 at 13:38
  • Many apologies, I mis-read it. It works perfectly! Thank you so much. – LeilaH Jul 2 '18 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.