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); ?>
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
@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