自定义查询每页只显示一个帖子

时间:2018-06-04 07:13:08

标签: php wordpress advanced-custom-fields

我正在尝试使用标记创建地图。代码工作正常,但它只显示每个帖子的一个结果。

<?php 
    $the_query_map = new WP_Query( array( 'post_type' => 'country', 'posts_per_page' => -1 ) );

    if($the_query_map->have_posts()) :
    while($the_query_map->have_posts()): $the_query_map->the_post();

    $the_ID = get_the_ID();

    if( have_rows('single_dealer') ):
    while ( have_rows('single_dealer') ) : the_row();

    $get_google_map = get_sub_field('map', $value);
    $marker_description = get_sub_field('description', $value);

    $output_map[$the_ID]['map'] = '<div class="marker" data-lat="'.$get_google_map['lat'].'" data-lng="'.$get_google_map['lng'].'">'.
    $marker_description.'</div>';

    endwhile; endif;
    wp_reset_postdata();
    endwhile; endif;


    ?><div class="acf-map"><?php
foreach( $output_map as $key => $map_marker ):
    echo $map_marker['map'];
    endforeach;
    ?>
</div>

如何让我的代码显示帖子的所有结果而不是一个?

1 个答案:

答案 0 :(得分:0)

你必须把循环结束wp_reset_postdata();

<?php 
    $the_query_map = new WP_Query( array( 'post_type' => 'country', 'posts_per_page' => -1 ) );

    if($the_query_map->have_posts()) :
    while($the_query_map->have_posts()): $the_query_map->the_post();

    $the_ID = get_the_ID();

    if( have_rows('single_dealer') ):
    while ( have_rows('single_dealer') ) : the_row();

    $get_google_map = get_sub_field('map', $value);
    $marker_description = get_sub_field('description', $value);

    $output_map[$the_ID]['map'] = '<div class="marker" data-lat="'.$get_google_map['lat'].'" data-lng="'.$get_google_map['lng'].'">'.
    $marker_description.'</div>';

    endwhile; endif;

    endwhile; 
 wp_reset_postdata();
endif;


    ?><div class="acf-map"><?php
foreach( $output_map as $key => $map_marker ):
    echo $map_marker['map'];
    endforeach;
    ?>
</div>