WordPress.org

Forums

Custom Field Output - Code Help Needed (4 posts)

  1. JaneLitte
    Member
    Posted 4 years ago #

    Goal: Display a rotating set of images linked to specific posts.

    Howto:

    1. Create a custom field called 'RotatingPostCover'
    2. In each post that I want to show up, an image link (http://blogname.com/wp-content/whatever.jpg') is inserted as the value of the custom field.
    3. In the code, I assign $postKey = get_post_custom_values('RotatingPostCover', $post->ID); and

    echo " <div class='rp_post_content'><img src='{$postKey}' /></div>\n";

    4. Currently, I am ending up with this output:
    <div class='rp_post_content'><img src='Array' /></div>

  2. Roger Theriault
    Member
    Posted 4 years ago #

    src='Array' means $postKey is an array.

    http://codex.wordpress.org/Using_Custom_Fields

    Sometimes the output is an array. If you want a list, use the foreach loop:

    http://codex.wordpress.org/Function_Reference/get_post_custom_values

    Or if you just want the first, use the older function:
    $postKey = get_post_meta('RotatingPostCover', $post->ID, true);

    http://codex.wordpress.org/Function_Reference/get_post_meta

  3. JaneLitte
    Member
    Posted 4 years ago #

    '$postKey = get_post_meta('RotatingPostCover', $post->ID, true);'

    This should populate $postKey with the value in 'RotatingPostCover' key for the current POST ID, correct? because when I outpute $postkey using this:

    echo " <div class='rp_post_content'><img src='{$postKey}' /></div>\n";

    I get nothing.

  4. Roger Theriault
    Member
    Posted 4 years ago #

    Do a bit of basic debugging...

    $postKey = get_post_custom_values('RotatingPostCover', $post->ID);
    and

    echo "ID=[". $post->ID . "]";
    var_dump($postKey);

    will tell you what is stored there, what the ID was that you though you had, and why you're getting nothing. Sometimes it's caused by some other code that ran earlier, changing your $post. Or maybe the first instance of your custom field is blank.

Topic Closed

This topic has been closed to new replies.

About this Topic