[resolved] Changing images in different pages (6 posts)

  1. Raman
    Posted 3 years ago #


    I am new to wordpress. After going through tutorials I am into development of a wordpress theme for my website, my question may sound dumb. I apologize for the same. I am facing a problem in understanding the following. I will be obliged to get any help / clarification in this regard.
    I understand that front-page.php is the template file that is considered for the static home page and other pages will follow index.php. I have a header.php, sidebar.php and footer.php.
    There are images that will be referred from with in the pages.
    My requirement is that there are images will change from page to page.

    Say in index.php I have a line like this.
    <td><img src="<?php bloginfo('template_directory') ?>/images/a.jpg" width="550" height="75"></td>

    But I don't want to display the same image in every page. I understand that while creating page from the dashboard "Add new page" I can not write lines like
    <td><img src="<?php bloginfo('template_directory') ?>/images/a.jpg" width="550" height="75"></td>
    in the editor. At least when I tried I failed to get the desired thing. There was too many errors in the generated final html.

    So I found a way that using if block I can check for the page ID in the index.php and accordingly display the image in the respective pages.
    But what if I have to build hundreds of pages. For every image in the page I'll end up having a switch case of 100 conditions. Is there any other way to achieve this?
    Apart from this huge if/else statement my theme will be tightly coupled with the page id removing the flexibility to add page on the fly at later stage.

    Can I write php code while adding page from the admin panel in the window provided?

    Thanks in advance.

  2. Michael
    Forum Moderator
    Posted 3 years ago #

    consider to use the 'featured image' for this purpose;


    for instance, define a thumbnail size 'header'; and then edit your posted line into:

    <td><img src="<?php if( is_page() && has_post_thumbnail($post->ID) ) : echo get_the_post_thumbnail($post->ID,'header'); else : bloginfo('template_directory') ?>/images/a.jpg<?php endif; ?>" width="550" height="75"></td>
  3. Raman
    Posted 3 years ago #

    Thanks a lot.
    It worked absolutely fine.

    I would like to ask you another thing. This worked for one image. But how can I support multiple such variable images in a page.
    Looking forward to your help once again.


  4. Michael
    Forum Moderator
    Posted 3 years ago #

    there is a plugin (which I have not used myself) to create more than one featured image for a page or post:


  5. Raman
    Posted 3 years ago #

    Thanks for the update.
    I'll use it and let you know how it goes.

  6. Raman
    Posted 3 years ago #

    I used that plugin and it worked perfectly fine.
    Thanks a lot for your kind suggestion.

Topic Closed

This topic has been closed to new replies.

About this Topic