• Does anyone know the code to display multiple values for a custom field?

    I have a link that I placed in my description of my post using the custom field. I tried to use the same custom field key to add another value to the same post but it doesn’t show. Does anyone know what code I have to add to make multiple values show?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I think what you need is to get the meta data (the data stored in custom fields) as an array, rather than a string:

    $myCustomField = get_post_meta($post->ID, 'myCustomFieldName', false);

    Thread Starter chea24

    (@chea24)

    Can you show me in this code? Also, I’m adding this to the content area of my post. But what is happening is the link is showing up on all posts. Can you show me how to properly make it so the custom field value only show for the post that I post it in?

    <p><a href="<?php echo get_post_meta($post->ID, 'youtube', true); ?>">Youtube</a></p>

    I believe this should work:

    <?php
    $youtubeURLs = get_post_meta($post->ID, 'youtube', false); //put all the youtube custom field values into an array
    
    if(!empty($youtubeURLs)) { //if the array is not empty
    
    //for each value in the array, display the link
    foreach ($youtubeURLs as $url){ ?>
    	<p><a href="<?php echo $url; ?>">Youtube</a></p>
    <?php
    	} //end foreach
    }//end if
    ?>

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to use a custom field for multiple values?’ is closed to new replies.