Support » Fixing WordPress » Calling a Custom Field Outside the Loop & Modifying

  • Resolved maniacaldesign

    (@gladneyc)


    Howdy –

    I have hit an inpass in the development in one of my client’s sites. I have implemented the “Custom Field Template” and can call custom fields both inside and outside of the WordPress loop.

    The issue I’m having is in trying to figure out how to conditionally add additional text, appended to the called field but ONLY if there is information available in that custom field.

    For example, if I have a custom field called keyed “numberbedrooms” with a value of “3”, how could I code the call to display the value followed by the word “bedrooms” but if the field has no value in it, it displays nothing. The code I am using to call the custom field outside the loop is as follows:

    <span class="meta">
    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    echo get_post_meta($postid, 'numberbedrooms', true);
    ?></span>

    I hope this makes sense, I’d be happy to clarify if not.

Viewing 3 replies - 1 through 3 (of 3 total)
  • esmi

    (@esmi)

    How about:

    <span class="meta">
    <?php
    global $wp_query;
    $postid = $wp_query->post->ID;
    if( get_post_meta($postid, 'numberbedrooms', true)) echo get_post_meta($postid, 'numberbedrooms', true) . ' bedrooms';
    ?></span>

    Esmi! Amazing! Worked like a charm. I’m still stumbling around php and its syntax. Thank you so much.

    esmi

    (@esmi)

    Glad I could help. 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Calling a Custom Field Outside the Loop & Modifying’ is closed to new replies.