Support » Fixing WordPress » array and get post meta

  • Hi Everybody,

    I have a problem I try to get meta data value to put them in an php array but doesn’t work well.

    I get my value called slidecolortochange but when I add it into my array the size is just 1 but my array should be 2.

    If I add directly my value into the array it works fine, i get the right size.

    What Im doing wrong?

    if ( get_post_meta($post->ID, 'slidecolortochange', false) ) : ?>
    <?php $slidecolortochange = get_post_meta($post->ID, 'slidecolortochange', true);
    	$slidecolortochange = trim($slidecolortochange);
        //echo $slidecolortochange; 
    	$test = array($slidecolortochange);
    	$size = count($test);
    	echo 'size : '.$size;
    endif; ?>


Viewing 3 replies - 1 through 3 (of 3 total)
  • I think the issue is that this code:

    <?php $slidecolortochange = get_post_meta($post->ID, 'slidecolortochange', true);

    should be changed to:

    <?php $slidecolortochange = get_post_meta($post->ID, 'slidecolortochange', false);

    The true in the first code block shown above tells WordPress to only retrieve a single meta value, even if multiple meta values are stored in the database.

    I’ve tried before but that give me an array into another one.

    I got that:

        [0] => 3=>'roro', 5=>'toto'
    size : 1

    And my custom field value is: 3=>’roro’, 5=>’toto’

    Maybe Im doing wrong. I just want to get a number associated with a name.

    Do you have another idea to make it?

    In that case, try:

    <?php $slidecolortochange = maybe_unserialize( get_post_meta($post->ID, 'slidecolortochange', true) );

    and see if that works for you.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘array and get post meta’ is closed to new replies.