在同一页面上显示两次 Wordpress 查询,一次没有分页,另一次有分页

Display Wordpress query twice on same page, 1 without pagination, the other with pagination

我在下面的 WordPress 网站上显示了医疗服务提供者的地图,并在这些提供者的下方显示了列表。

https://preview.scmamit.yourmark.com/?sfid=756&_sft_provider_type=primary-care

我需要在地图上显示所有搜索结果,但列表应该有分页(每页只显示 25 个)。我 运行 循环了两次,第一次是为地图构建图钉,第二次是显示列表。

如果我关闭分页以显示地图上的所有图钉,那么如何在地图下方显示分页列表?

回到我的 Coldfusion 时代,我会为第二个循环做一个查询查询,我已经看到 "pre_get_posts" 和其他一些看起来接近我想要的选项,但没有什么是完全的带我去那里。

只是 运行 第二次循环:

首先在其末尾重置第一个查询:

wp_reset_query();

并将其添加到您的第二个查询中:

$count = get_option('posts_per_page', 10);
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$offset = ($paged - 1) * $count;

并将此参数添加到查询中:

'posts_per_page' => $count,
'paged' => $paged,
'offset' => $offset,

问候汤姆

以为我会 post 我做了什么来完成这项工作。感谢@tom-ukelove 让我朝着正确的方向前进。

我在这个页面上有地图、列表和搜索过滤器。搜索过滤器是使用搜索和过滤器插件创建的。

https://scmamit.com/?sfid=756

在我的搜索和过滤器设置中,我将每页的结果设置为 25,这是我需要列表在循环 #2 中执行的操作。

为了在地图上显示所有结果(循环 #1),我使用以下代码删除了 25 posts_per_page 限制。

<?php
//get the search variables from URL
$this_keyword = $_GET['_sf_s'];
$this_provider_type = $_GET['_sft_provider_type'];
$this_city = $_GET['_sfm_city'];

//rebuild the query
$args = array(
'post_type' => 'providers',
'provider_type' => $this_provider_type,
's' => $this_keyword,
    'meta_query' => array(
        array(
            'key' => 'city',
            'value' => $this_city,
            'compare' => 'LIKE'
        )
    ),
//use -1 to remove the 25 results per page limit
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC'
);

$the_query = new WP_Query( $args ); 
global $wp_query;
// Put 25 limit query object in a temp variable
$tmp_query = $wp_query;
// Now purge the global query
$wp_query = null;
// Re-populate the global with the no-limit custom query
$wp_query = $the_query;
$count = $the_query->post_count;

//run the loop #1 and build map pins within.
while($the_query->have_posts()) : $the_query->the_post();

//echo out map pin code here, or whatever you need in the 1st loop.

endif;
endwhile;

//reset the query
wp_reset_query();

// Restore original 25 limit query object
$wp_query = null;
$wp_query = $tmp_query;
?>

<ul>
<?php
// run the 2nd loop with pagination
while(have_posts()) : the_post();

// some <ul> list here

endwhile;
?>
</ul>

<?php 
//display pagination
$numeric_posts_nav();
?>

希望对以后的人有所帮助。 Lmk如果有更有效的方法。