• Hi there

    Just learning WP and I would appreciate your help with this. I have number of custom posts in my wordpress and I need to pull custom filed from each type of post to homepage.

    Now first post type have field ‘imag1’, second type have ‘imag2’, 3rd, ‘imag3’ etc. How can I call all those values to be shown in one spot. So I want to say show imag1 or imag2 or imag3 – which ever is related ot current post.

    I know how to call one custom field value, works perfectly with:
    <?php echo get_post_meta($post->ID, “imag1”, true); ?>

    But how do I call to show any of above?

    Thx a lot good people!

Viewing 6 replies - 1 through 6 (of 6 total)
  • hi dean_l

    You can use this
    <?php get_post_custom($post_id); ?>

    or make an array of your metakeys and thne start loop and in that loop write your code mean

    <?php echo get_post_meta($post->ID, “imag1”, true); ?>

    Moderator keesiemeijer

    (@keesiemeijer)

    get_post_meta(); returns an empty string if the Post doesn’t have the custom field. You can test it like so:

    $image_meta = get_post_meta($post->ID, "imag1", true);
    if($image_meta != '') {
    // post has imag1 meta value -> do stuff
    }

    But why do they all have to be different (imag1, imag2, ‘imag3’) when they (probably) do the same thing?

    This may work not tested

    <php?
    $post_meta = get_post_meta($post->ID);
    foreach ($post_meta as $output) :
    	$pos = strpos($output, 'imag');
    	// Note our use of ===, because == would not work as expected
    	// the position of 'imag' is the 0th (first) character.
    	if ($pos === true) {
    		echo get_post_meta($post->ID, $output, true);
    		return;
    	}
    endforeach;
    ?>

    HTH

    David

    Moderator keesiemeijer

    (@keesiemeijer)

    @digital Raindrops
    I think the second argument ($key) is required:
    http://codex.wordpress.org/Function_Reference/get_post_meta

    But maybe I’m wrong.

    Thanks keesiemeijer,

    I think the second argument ($key) is required:

    Quite correct, however I had to investigate, this code works and I have tested it, where it selected the correct value (5) from these custom fields.

    Array(
    [0] => _thumbnail_id ($pos = null)
    [1] => _edit_lock ($pos = null)
    [2] => _edit_last ($pos = null)
    [3] => thumbnail_image ($pos = 10)
    [4] => the_image ($pos = 4)
    [5] => image_1 ($pos = 0)
    [6] => iimage_1 ($pos = 1)
    )

    Returns image_1 value “Image number 1”

    <?php $post_meta = get_post_custom();
    	$keys = array_keys($post_meta);
    	foreach ($keys as $output) :
    		$pos = strpos($output, 'imag');
    		// Note our use of === 0, because == would not work as expected returns null or false
    		// the position of 'imag' is the 0th (first) character.
    		if ( $pos === 0 ) {
    			echo get_post_meta($post->ID, $output, true);
    			return;
    		}
    	endforeach;
    ?>

    Interesting that the ‘_thumbnail_id’ is stored in the post meta!
    [_thumbnail_id] => Array( [0] => 227 )

    HTH

    david

    Thread Starter dean_l

    (@dean_l)

    Great. Thanks a lot Sangram, Kessie and David.
    I trying to make kind of custom product listing, so lot of values. This will help to sort them out and display on the homepage. Cheers

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Calling 5 custom field values at once???’ is closed to new replies.