WordPress.org

Ready to get started?Download WordPress

Forums

Image that changes depending on the Page Id (7 posts)

  1. adzay
    Member
    Posted 1 year ago #

    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

    http://www.transformationpowertools.com/wordpress/different-image-for-each-page-in-wordpress#comment-14876

    <?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="" />'; ?>
    </div>

    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,
    Thanks

  2. Rebecca Schaefer
    Member
    Posted 1 year ago #

    What is showing up in the source? Is it even trying to load that image or is it loading the default?

  3. adzay
    Member
    Posted 1 year ago #

    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.

  4. webmarshall
    Member
    Posted 1 year ago #

    adzay - did you get this resolved? I'm dealing with the same problem.

    Thanks!

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

    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();
    $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="" />'; ?>
    </div>
    <?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.

  6. adzay
    Member
    Posted 1 year ago #

    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 http://www.linktwitch.com 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.

  7. webmarshall
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic