2012-02-15 75 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; 
?>