Support » Themes and Templates » Image that changes depending on the Page Id

  • Hi all,
    I want to have image, not a header image, that changes depending on the page id.

    I found this code snippet on this website

    Different header image for each page in wordpress

    <?php $page_id=get_the_ID();
    if(is_page()) { $image='head-image-'.$page_id.'.jpg'; };
    if(!file_exists(STYLESHEETPATH.'/images/'.$image)) { $image='head-image.jpg'; }
    echo '<img src="'.get_stylesheet_directory_uri().'/images/'.$image.'" alt="" />'; ?>

    I followed the instruction on the page. but it still did not work. Im wondering if it’s because I’m using a png instead of a Jpeg. How ever, no matter what I try I just can’t get it to work. ( Do i need to define “STYLESHEETPATH” SOMEWHERE?)

    Unfortunately it is not online yet so i cannot show you. But I plan to put it in the index.php file. It is placed just above the “content” and will be 3d images of “Blog” “Video” etc.

    This is how it is in my index file:

    <div id="Pagetitleimage"><?php $page_id=get_the_ID();
    if(is_page()) { $image='head-image-'.$page_id.'.png'; };
    if(!file_exists(STYLESHEETPATH.'/images/'.$image)) { $image='head-image.png'; }
    echo '<img src="'.get_stylesheet_directory_uri()'/images/'.$image.'" alt="" />'; ?>

    I understand about Css styling, I just want my page to find the images I have made in the first place so that I can apply the styling

    Please help out,

Viewing 6 replies - 1 through 6 (of 6 total)
  • What is showing up in the source? Is it even trying to load that image or is it loading the default?

    Hi Rebecca, Nothing is loading at all. It shows that broken image icon. Upon further inspection with Firebug, “Img Src” does not show the complete url of the image.

    adzay – did you get this resolved? I’m dealing with the same problem.




    Forum Moderator

    code rewritten to move the html markup into the conditional ‘is_page()’ statement:

    <?php if(is_page()) { ?>
    <div id="Pagetitleimage"><?php $page_id=get_the_ID();
    if(!file_exists(STYLESHEETPATH.'/images/'.$image)) { $image='head-image.png'; }
    echo '<img src="'.get_stylesheet_directory_uri().'/images/'.$image.'" alt="" />'; ?>
    <?php }; ?>

    this should avoid the ‘broken image’ symbol in any other than static pages.

    also make shure to save the images into the /images folder of your theme, and to have a default image, and to replace the .png in all places if your are using .jpg images.

    Ah Alchymyth ( the legend) to be honest I was hoping you would receive your help on this. However, its a bit too late. After posting, I realized and decided not to give users anymore pictures to download and used text instead. My website is a child theme. I decided to write VIDEO GALLERY and BLOG instead of using pictures .

    I hope the others in this this thread find their answers.

    Thank you very much alchymyth! I’ll give it a shot.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Image that changes depending on the Page Id’ is closed to new replies.