WordPress image targeting

This is a small code tip for WordPress templating which helps better target images in WordPress templates.

Code lines are easy to remove or add, based on request. Targeting is secured by WordPress PHP slug is_home, is_single, is_page, is_archive, is_page.

<?php if (is_home()) { ?>
<img src="<?php bloginfo('template_url'); ?>/img/home_dogs.jpg" >
<?php } elseif (is_single()) { ?>
<img src="<?php bloginfo('template_url'); ?>/img/single_dogs.jpg" >
<?php } elseif (is_page()) { ?>
<img src="<?php bloginfo('template_url'); ?>/img/default_famous_dogs.jpg" >
<?php } elseif (is_archive()) { ?>
<img src="<?php bloginfo('template_url'); ?>/img/archive_dogs.jpg" >

// Targeting on certain page, based on its slug.
<?php } elseif (is_page('dog-lessie')) { ?>
<img src="<?php bloginfo('template_url'); ?>/img/lessie.jpg" >
<?php } elseif (is_page('dog-laika')) { ?>
<img src="<?php bloginfo('template_url'); ?>/img/laika.jpg" >

// Default picture
<?php } else { ?>
<img src="<?php bloginfo('template_url'); ?>/img/default.jpg" >
<?php } ?>
This entry was posted in Projects and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.