WordPress.org

Ready to get started?Download WordPress

Forums

Add different image in sidebar on each PAGE (8 posts)

  1. bitsyandkitty
    Member
    Posted 4 years ago #

    Hi,

    I'm looking to add a different image into the sidebar for each Page (not post) - each image is specific to a page - not random.
    Any ideas?
    I've tried all sorts!
    thanks,
    Bitsy x

  2. racer x
    Member
    Posted 4 years ago #

    You can use custom fields for this.

    You have to pay attention to where your loop is however. If your sidebar comes before or after the wp loop(which it usually does) then you can use this snippet to get the custom field value.

    Or you can search the plugins for a widget.

  3. bitsyandkitty
    Member
    Posted 4 years ago #

    Thanks racer but I'm not so clear - so do i add this code to the sidebar.php

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'customField', true);
    ?>

    ?
    change customField for something like 'page-imag'e, create the custom filed in the page and add the image?

    i, surprisingly, drew a blank on widgets that do this simple - seemingly - function? There's room for one out there you know!.

    thanks,
    Best, Bitsy x

  4. racer x
    Member
    Posted 4 years ago #

    Yep. When making the page in the custom field section under "name" input the name of the field. (You will only need to do this once. Then it becomes part of the drop down choices.) In the "value" side input the full url to the image path previously uploaded somewhere on your site files(or uploads).

    I like to use a conditional statement then(in your case in the sidebar where you want the image to appear) so I can do something if no image was set. (Like show a default image, etc.)

    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    $image = get_post_meta($postid, 'page_image', true);
    if ( $image != '' ) { ?>
       <img src="<?php echo $image; ?>" />
    <?php } else { ?>
       <img src="put an alternate image here" />
    <?php } ?>
  5. bitsyandkitty
    Member
    Posted 4 years ago #

    thanks racer - that's working great. all very easy when you know how eh!

    many many thanks for the code.
    xx

  6. racer x
    Member
    Posted 4 years ago #

    Glad it worked out!

  7. bitsyandkitty
    Member
    Posted 3 years ago #

    Hi,

    i'm hoping Racer X is out there as you help was really fab previously.

    I am using a theme which has a home page pulling up the latest posts - i want this to pull up pages instead. How can i change the code to do that? It has four sections like so;

    [Code moderated as per the Forum Rules. Please use the pastebin]

    any ideas? or anybody else for that matter!

  8. faxon22
    Member
    Posted 3 years ago #

    just wanted to say thank you for this tutorial. This is such a huge help. Many thanks

Topic Closed

This topic has been closed to new replies.

About this Topic