簡體   English   中英

wordpress get_post_meta無法循環工作

[英]wordpress get_post_meta not working in loop

我試圖在自定義字段“價格”訂購的頁面上發布帖子我已經完成了訂購,但現在我無法得到'價格'的價值回應。 get_post_meta不提供任何輸出。 這是代碼:

$args=array(
'meta_key'=>'price',
'post_type' => 'page',
  'orderby'=>'meta_value_num',
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1

);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
 $count=0;
  while ($count<4 && $my_query->have_posts()) : $my_query->the_post(); ?>

    <td><a href="<?php the_permalink(); ?>">
    <img alt="product" src="/product-images/image.jpg" height="100px" width="75px"/>
    <p><?php the_title(); ?></p>
    <?php echo get_post_meta($my_query->ID, 'price', true); ?>
    </a>
    </td>
    <?php
    $count++;
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>

您正在嘗試在WP_Query$ID )上使用屬性而不是當前帖子的ID。 get_post_meta的第一個參數應該是帖子ID,而不是WP_Query的屬性。

如果這是在模板中的某個位置,您可以這樣做:

<?php
while ($my_query->have_posts()) {
    $my_query->the_post();

    // use the global $post object
    echo get_post_meta($post->ID, 'price', true);
}

如果在模板文件中或在聲明全局$post地方沒有,則可以使用get_the_ID

<?php
while ($my_query->have_posts()) {
    $my_query->the_post();

    // use the global $post object
    echo get_post_meta(get_the_ID(), 'price', true);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM