• Resolved curiousbird

    (@curiousbird)


    Hi,

    I am a total newbie at this thing, but i have trouble at displaying multiple values of one custom field key, I am using ACF plugin and have in the loop the following code:

    Type of sample: <?php $key = get_post_meta($post->ID, 'sample_case', false);
    			print_r($key); ?>

    this is what is displayed on the site:

    Type of sample: Array ( [0] => Array ( [0] => Sample case 1 [1] => Sample case 2 ) )

    I would like it to look like this:

    Type of sample: Sample case 1, Sample case 2

    How can i do this, I searched the internet and can’t seem to find a solution that would do something like this.

    Thanks in advance for any help

Viewing 5 replies - 1 through 5 (of 5 total)
  • What type of field you created in ACF and what value you are inserting? You can change where you have “false” to “true” then the return will be a string and not an array. But the ACF has two functions to display the results get_field () and the_field (), try replacing your code with the_field ('sample_case');

    Thread Starter curiousbird

    (@curiousbird)

    Thanks Leo for your input.

    I know if I put the $single to “true” that it works, but it works only with showing up one value. In my case either Sample case 1 or Sample case 2, but that option can’t show up both at the same time.

    In ACF I created Select (under Choice) and the values I inserted in the Choices field are like this:

    Sample case 1 : Sample case 1
    Sample case 2 : Sample case 2
    Sample case 3 : Sample case 3

    replacing my code with the_field (‘sample_case’);
    where should I put that?

    Thanks

    Type of sample: <?php the_field( 'sample_case' ); ?>

    Thread Starter curiousbird

    (@curiousbird)

    Thanks Leo for your input, I tried all those but didn’t work.
    I got this from Tanushka and it works:

    <?php $key = get_post_meta($post->ID, 'sample_case', false);
    			$first = true;
    			foreach( $key[0] as $k){
    				if($first){
    					echo $k;
    					$first = false;
    				}else{
    					echo ', '.$k;
    				}
    
    			}
    
    			?>

    It works like a charm ??

    Good to know that solved your problem!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Trouble displaying multiple values of a custom field key’ is closed to new replies.