0

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

I am not getting what the error is.Someone please help in this

4

2 回答 2

1

get_post_custom_values()如果键不存在,则返回null 。参考:wp-includes/.post.php:1892

您可以将返回的值转换为数组,以确保您始终在处理数组并避免该警告:

<?php $key_values = (array) get_post_custom_values("Description"); ?>
于 2013-02-26T13:36:42.490 回答
0

为什么不使用get_post_meta?我在我的网站上使用它,它按预期工作。

于 2013-02-26T13:33:26.340 回答