2013-02-26 89 views
0

我製作了一個自定義模板,用於在單個頁面中顯示「產品」類別中的帖子。我有一些自定義字段。我需要在頁面中顯示自定義字段的值。我已經使用了get_post_custom_values爲以下但我得到一個錯誤get_post_custom_values無法正常工作

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

爲products.php的代碼如下。

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

我沒有得到什麼錯誤is.Someone請在此

回答

0

幫助,爲什麼不使用get_post_meta?我在我的網站上使用它,它按預期工作。

1

get_post_custom_values()返回null如果密鑰不存在。 編號:WP-包括/ .post.php:1892年

你可以返回的值轉換爲數組,以確保你總是處理一個陣列,並避免警告:

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