WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Output Different Custom Fields Separately

[Resolved] Output Different Custom Fields Separately

  • I have two custom fields in a post,

    customfiled-a
    customfiled-b

    in my single.php file, I wish to place each output in a div class …

    <div class=“a”>
    customfiled-a
    </div>
    <div class=“b”>
    customfiled-b
    </div>

    How can I do this? I have tried Get_post_meta() but it prints out both as expected 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Have you tried it like this (inside the loop):

    <?php
    $customfiled_a = get_post_meta($post->ID, 'customfiled-a', true);
    if ($customfiled_a != '' ) : ?>
    <div class="a">
    <?php echo $customfiled_a; ?>
    </div>
    <?php endif; ?>

    Snap!

    Get single post meta!

    get_post_meta( $post_ID, $key , $single );

    <div class='a'><?php echo get_post_meta(get_the_ID(), 'field-a', true); ?></div>
    <div class='b'><?php echo get_post_meta(get_the_ID(), 'field-b', true); ?></div>

    HTH

    David

    I have tried the two and the result comes up blank. It only seems to work with get_post_meta(), could I be doing something wrong?

    Try:

    <?php global $post; ?>
    <div class='a'><?php echo get_post_meta($post->ID, 'customfiled-a', true); ?></div>
    <div class='b'><?php echo get_post_meta($post->ID, 'customfiled-b', true); ?></div>

    HTH

    David

    Sorry guys, they are both working. It was an issue of “too much caching”.

    Cleared all the caches and things are OK.

    Thank you

    Pls I am new to wordpress. Can someone please tell me where to locate the file where I can include the php shortcode above? I am using custom field template and advanced custom field plugins

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘[Resolved] Output Different Custom Fields Separately’ is closed to new replies.
Skip to toolbar