WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Output Different Custom Fields Separately (7 posts)

  1. maxbuxfiy
    Member
    Posted 2 years ago #

    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 :-)

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    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; ?>
  3. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  4. maxbuxfiy
    Member
    Posted 2 years ago #

    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?

  5. Digital Raindrops
    Member
    Posted 2 years ago #

    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

  6. maxbuxfiy
    Member
    Posted 2 years ago #

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

    Cleared all the caches and things are OK.

    Thank you

  7. oezidi
    Member
    Posted 2 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic