• Resolved Lolley

    (@lolley)


    I would like to be able to have unique pictures in the sidebar of several pages on my site.

    I have discovered that I could possible use custom fields to do this. I have read that I can add code to the sidebar php (in my case I am using a child theme of Travelify and wish to add the custom field to sidebar-right.php)

    The code I found is:

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

    Further investigation told me that I should modify ‘customField’ to something more meaningful, so I chose ‘pageImage’. I added this to the end of the sidebar-right.php code.

    On my page, I added the new custom field: “pageImage” and gave it a value of the URL of the required image stored in my media.

    On viewing the page, in the place of the sidebar is the URL of the image. I feel like I’m halfway there, but need some expertise to complete the task!

    This is what my sidebar-right.php looks like:

    <?php
    /**
     * Displays the right sidebar of the theme.
     *
     */
    ?>
    
    <?php
    	/**
    	 * travelify_before_right_sidebar
    	 */
    	do_action( 'travelify_before_right_sidebar' );
    ?>
    
    <?php
    	/**
    	 * travelify_right_sidebar hook
    	 *
    	 * HOOKED_FUNCTION_NAME PRIORITY
    	 *
    	 * travelify_display_right_sidebar 10
    	 */
    	do_action( 'travelify_right_sidebar' );
    ?>
    
    <?php
    	/**
    	 * travelify_after_right_sidebar
    	 */
    	do_action( 'travelify_after_right_sidebar' );
    ?>
    
    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'pageImage', true);
    ?>

    [Moderator Note: Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

Viewing 6 replies - 1 through 6 (of 6 total)
  • http://www.w3schools.com/TAGS/tag_img.asp

    for example, check first if the custom field is set, and then output it into an html img tag:

    if( get_post_meta($postid, 'pageImage', true) ) echo '<img src="' . get_post_meta($postid, 'pageImage', true) . '" alt="" />';

    Thread Starter Lolley

    (@lolley)

    Thank you, but would you mind holding my virtual hand as I don’t know what to do with the code to check that the custom field is set or output it into an html img tag!

    the suggested code is supposed to replace this line in your code:
    echo get_post_meta($postid, 'pageImage', true);

    Thread Starter Lolley

    (@lolley)

    Aha! So I’ve done that, created a custom field on my page named pageImage and given it the value of the URL of one of my images in the media file and this gives me an image icon where I’d like my image to be… how would I get an image to show?

    if you are using the right full url for the image, that should actually give you an image to view.

    Thread Starter Lolley

    (@lolley)

    Correct! I wasn’t! I have to open the image in the browser and copy the URL there rather than using the permalink.

    Yippee! It works. Thank you very much for your help.

    I have one other question: If I wanted two images in the sidebar, how would I handle that?

    🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How do I add a custom field to a sidebar?’ is closed to new replies.