我试图在自定义字段"价格"订购的页面上发布帖子我已经完成了订购,但现在我无法得到'价格'的价值回应.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().
?>
Run Code Online (Sandbox Code Playgroud)
chr*_*guy 10
您尝试在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);
}
Run Code Online (Sandbox Code Playgroud)
如果在模板文件中或在$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);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14625 次 |
| 最近记录: |