首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的WP查询不返回第一个结果的后置元?

为什么我的WP查询不返回第一个结果的后置元?
EN

WordPress Development用户
提问于 2018-01-29 15:12:29
回答 1查看 89关注 0票数 0

我正在创建一个用于返回来自自定义post类型的帖子以及特定ACF字段的短代码。一切都很好,除了第一个帖子没有返回它的帖子元信息。

我的代码:

代码语言:javascript
复制
$args = array(
    'post_type' => 'show',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby'   => 'meta_value',
    'meta_key'  => 'show_date',
    'order' => ASC
);

$output = '';

$query = new WP_Query( $args );
if( $query->have_posts() ){

    $output .= 'The Showlist';

    while( $query->have_posts() ){

        $show_date = get_field('show_date');
        $show_venue = get_field('show_venue');

        // print '';
        // print_r($show_date);
        // print '';

        $query->the_post();
        $output .= '' . get_the_title() . ' @ ' . $show_venue->post_title . '';
        $output .= '' . $show_date . '';
    }

}
wp_reset_postdata();

// do shortcode actions here
return $output;

我得到的一个例子是:

  • 展览名称-
  • 展览名称-展览地点-展览日期
  • 展览名称-展览地点-展览日期
  • 展览名称-展览地点-展览日期
  • 展览名称-展览地点-展览日期
  • 展览名称-展览地点-展览日期
  • 展览名称-展览地点-展览日期

我不知道为什么第一项不返回元信息。我要再次检查它是否包含后端的信息。

有什么想法吗?蒂娅。

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2018-01-29 15:18:48

代码语言:javascript
复制
    $show_date = get_field('show_date');
    $show_venue = get_field('show_venue');

    // print '';
    // print_r($show_date);
    // print '';

    $query->the_post();

在获得值之后,您正在设置活动的post。

$query->the_post()始终放在other循环的顶部,以便其他函数可以依赖正确设置的全局$post变量。

票数 3
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/292503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档