WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Raman
    Member
    Posted 1 year ago #

    Hello,

    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. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

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

    http://codex.wordpress.org/Post_Thumbnails

    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
    Member
    Posted 1 year ago #

    Thanks a lot.
    Amazing.
    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.

    Regards,

  4. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

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

    http://wordpress.org/extend/plugins/multiple-featured-images/

  5. Raman
    Member
    Posted 1 year ago #

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

  6. Raman
    Member
    Posted 1 year 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