2012-02-15 76 views
1

我在一个名为'Show'的自定义帖子类型下做了一些meta_values。我已经确认元值正在DB中正确存储。所以,现在,我有下面的代码片段:Wordpress get_post_meta() - 不是返回值

<?php 
     $args = array('post_type' => 'show', 'posts_per_page' => 1); 
     $loop = new WP_Query($args); 
     while ($loop->have_posts()) : $loop->the_post(); 
      print get_post_meta($loop->ID, 'date_meta', true); 
      the_title(); 
     endwhile; 
?> 

实际的循环工作的酒,因为它显示the_title的结果()。但是get_post_meta()没有返回任何东西。关键值是正确的,并且在数据库中有一个值。

另外,如果我尝试打印$环 - > ID,它不返回任何东西要么...

想法?

回答

1

添加一个引用到全局$后变量需要:

<?php 
    $args = array('post_type' => 'show', 'posts_per_page' => 1); 
    $loop = new WP_Query($args); 
    while ($loop->have_posts()) : $loop->the_post(); 
     global $post; 
     print get_post_meta($loop->ID, 'date_meta', true); 
     the_title(); 
    endwhile; 
?> 
0
<?php 
$args = array('post_type' => 'show', 'posts_per_page' => 1); 
$loop = new WP_Query($args); 
while ($loop->have_posts()) : $loop->the_post(); 
    echo get_post_meta($loop->post_ID, 'date_meta', true); 
    the_title(); 
endwhile; 
?>