I have been trying to figure out to display multiple custom fields with the same value and do not display them if there is no value entered. This is the piece of code I'm currently using and it outputs values when the person has not entered their Instagram profile URL as well. All of this happens in a loop for a custom post type and there are otherwise no issues.

So, basically, I'm looking for a way to show as many values as there are for as long as there are some or don't show anything otherwise. And the values are always URLs in this case.

Thank you!

$profiletwitter = get_post_meta($post->ID, 'profiletwitter', true);
if ( ! empty( $profiletwitter ) ) { ?>
    <span class='social-profile'>
        <a href="<?php echo esc_attr( $profiletwitter ); ?>" target='blank'>
            <img src='http://blogomanija.net/sajt/wp-content/themes/blogomanija/images/social/twitter-small.png' />
  • What do you mean image src doesn't matter? You want several same images that point to different URIs?
    – N00b
    May 7, 2016 at 21:38
  • And data model? What exactly is $profileinstagram? Array of URIs? Object? How do you get it?
    – N00b
    May 7, 2016 at 21:43
  • @N00b I don't want to show the website it's on, just that. The images work fine. $profileinstagram should output a single URL, but some people have multiple profiles on instagram, so there should be two instances of this. What goes before this bit is ` <?php $profiletwitter = get_post_meta($post->ID, 'profiletwitter', true); if ( ! empty( $profiletwitter ) ) { ?> ` When set to FALSE, I get the word Array instead of an URL. When I use TRUE and a foreach loop, all fields are displayed, regardless of there being one value, more values or none.
    – Iva
    May 7, 2016 at 22:01
  • I added what was missing to the OP. I apologise and hope it's clearer now. Thank you for letting me know that the information was not sufficient.
    – Iva
    May 7, 2016 at 22:07

1 Answer 1


// False: get all, true: get only first
// Use false if you want to get all, if you have only one, it doesn't break
$profile_twitter = get_post_meta( $post->ID, 'profiletwitter', false );

// Let's check if there's any
if( ! empty( $profile_twitter ) ) { ?>

    <span class='social-profile'> <?php

        // Check if an array -> has more than one
        if( is_array( $profile_twitter ) ) {

            // Loop through each twitter url
            foreach( $profile_twitter as $single_twitter ) { ?>

                <a href="<?php echo $single_twitter; ?>" target='blank'>
                    <img src='http://blogomanija.net/sajt/wp-content/themes/blogomanija/images/social/twitter-small.png' />
                </a> <?php 
        // Not an array, it must be single (if it's correctly stored in db)
        else { ?>

            <a href="<?php echo $profile_twitter; ?>" target='blank'>
                <img src='http://blogomanija.net/sajt/wp-content/themes/blogomanija/images/social/twitter-small.png' />
            </a> <?php 
        } ?>

    </span> <?php 
} ?>

More reading:

get_post_meta(), is_array(), foreach

  • Thank you so, so, so much! Would give you +100 if I could. Works like a charm and is precisely what I need. And now I really need to brush up my knowledge of conditional statements in general! The comments you provided within code will be more than helpful . :)
    – Iva
    May 7, 2016 at 23:11
  • Im glad I could help. Don't forget to press accept answer checkmark! Good studying!
    – N00b
    May 7, 2016 at 23:14
  • Just did. I was so caught up in leaving pluses everywhere that I forgot the most important thing, that checkmark. D'oh! :)
    – Iva
    May 7, 2016 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.