• I’m a PHP newbie, so please be gentle.
    I want to display different images on each PAGE’s sidebar. I named each image the same name as the page title. I tried using this tag in the PHP widget:

    <img src="<?php bloginfo('template_url'); ?>/images/<?php wp_title(); ?>.jpg">

    …and about a hundred other combinations, but nothing works…

    I could do the whole if-else statement thing for every page on the site, but this seems like such a simple solution…if only it would work!
    Please…any help would be greatly appreciated.

Viewing 1 replies (of 1 total)
  • You’re probably better off using a custom field to achieve this.

    Start by going into the page editor for the page in which you want an image to appear. Upload the image, and copy the image location (the full URL)

    In custom fields, create a new custom field, called, e.g. ‘Sidebar Image’ (without the quotes) and in the value field paste the URL. Add the custom field and update the page.

    Now you need to make the theme template look for the custom field. Putting something like the following into your sidebar code should do the trick:

    <?php
    if( (is_page()) || (is_single()) ):
    
    $sidebar_img = get_post_meta($post->ID, "Sidebar Image", $single = true);
    if($sidebar_img){
    echo('<div class="sidebar-img-container">
    <img src="' .$sidebar_img. '" alt="Sidebar Image" class="sidebar-img" />
    </div>');
    }
    
    endif;
    ?>

    That should do it. Obviously you can change the output HTML to whatever you want, as long as the img src references the custom field value.

    This way, you have the option of easily swapping out images on a page-by-page or post-by-post basis.

    Good luck!

    John

Viewing 1 replies (of 1 total)
  • The topic ‘Use page title to call an image’ is closed to new replies.