• Resolved rw1

    (@rw1)


    hello,

    i am trying to output the contents of a custom field (created through the More Field plugin) in the head area of my templates header.php with:

    <?php if ( in_category('5') ) {echo '<meta property="text" content="' . get_post_meta(get_the_ID(), 'custom_field', true) . '" />';}?>

    and it is currently outputting:

    <meta property="text" content="" />

    ie it is not grabbing the value in the ‘custom_field’.

    can someone please tell me how i can successfully grab the value of that custom field?

    thanks very much!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The issue is that you are calling this outside of the loop. Thus the posts have not been set up yet.

    According to the codex
    http://codex.wordpress.org/Function_Reference/get_the_ID
    get_the_id()

    Returns the numeric ID of the current post. This tag must be within The Loop.

    When in header.php the loop has not yet been initiated. There is not yet a post ID # established so get_the_id() is returning null or false, i.e. nothing.

    You need a way to pass to header.php the post id of the post you want displayed on the page you are in the process of displaying.

    Thread Starter rw1

    (@rw1)

    yes thank you, just in the process of posting this solution, thanks to this post.

    so the code that worked was:

    <?php if (in_category('5') ) { the_post(); rewind_posts();
    echo '<meta property="text" content="' . get_post_meta(get_the_ID(), 'custom_field', true) . '" />';
    } ?>

    thanks for your reply!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘how to call a custom field in the head area?’ is closed to new replies.