Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have made a custom template to show the posts in "Products" category in a single page.I have some custom fields.I need to show the values of the custom fields in the page. I have used the get_post_custom_values as the following but i get an error as

Warning: Invalid argument supplied for foreach() in C:\wamp\www\SampleSite\wp-content\themes\TwentyElevenChildTheme\products.php on line 11

The code for products.php is as follows.

<?php
/*
Template Name: Product Template
*/
?>
<?php get_header(); ?>
<div id="content" class="narrowcolumn">
  <?php $recent = new WP_Query("cat=4&showposts=10"); while($recent->have_posts()) :$recent->the_post();?>
  <?php $key_values = get_post_custom_values("Description"); ?>
  <?php foreach($key_values as $key => $value )?>
  <?php echo  "$key  => $value("Description") <br />";?>
  <?php endwhile; ?>
</div>
<?php get_footer(); ?>

I am not able to find what the error is.Someone please help me.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

It most probably means that no custom post values have been retrieved for the key Description that you used. According to the Codex, Returns nothing if no such key exists, or none is entered.

Try adding print_r($key_values); on the line just after get_post_custom_values("Description"); and check if any value is retrieved.

To skip this warning(when you don't have any custom post values associated to a post), you should check if $key_values is an array before doing the foreach. And don't forget to escape your double quotes.

if( is_array( $key_values ) ){
  foreach($key_values as $key => $value );
  echo  "$key  => $value(\"Description\") <br />"; #You should escape your double quotes here!
}
share|improve this answer
    
Thank you very much...i got it –  MidhuN Feb 26 '13 at 13:46
add comment

You can try

<?php $key_values = get_post_custom_values("Description",get_the_ID()); ?>

ps: you should use posts_per_page instead of showposts in WP_Query().

You can use also debug it with

 print_r(get_post_custom(get_the_ID()));

to find out all the custom fields in your post.

http://codex.wordpress.org/Function_Reference/get_post_custom

share|improve this answer
    
Thank you very much for the suggestion..!!!! –  MidhuN Feb 26 '13 at 15:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.