简体   繁体   English

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

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

I have the WordPress site below that displays a map of medical providers and a list below that of those same providers.我在下面有一个 WordPress 站点,它显示了医疗提供者的 map 以及这些相同提供者的列表。

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

I need to show all of the results of a search on the map, but then the list should have pagination (only displaying 25 per page).我需要在 map 上显示所有搜索结果,但是列表应该有分页(每页只显示 25 个)。 I'm running through the loop twice, 1st to build the pins for the map, and 2nd to display the list.我在循环中运行了两次,第一次为 map 构建引脚,第二次显示列表。

If I turn off pagination to show all the pins on the map, how then would I display the list with pagination below the map?如果我关闭分页以显示 map 上的所有引脚,那么我将如何在 map 下方显示带有分页的列表?

Back in my Coldfusion days, I would have done a query of queries for the 2nd loop, and I've seen "pre_get_posts" and some other options that seem close to what I want, but nothing is quite getting me there.回到我的 Coldfusion 天,我会查询第二个循环的查询,我已经看到“pre_get_posts”和其他一些似乎接近我想要的选项,但没有什么能让我到达那里。

Just run the loop a 2nd time:只需第二次运行循环:

First reset the 1st query at the end of it:首先在其末尾重置第一个查询:

wp_reset_query();

And add this to your second query:并将其添加到您的第二个查询中:

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

And add this args to the query:并将此参数添加到查询中:

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

Regards Tom问候汤姆

Thought I would post what I did to get this work.以为我会发布我为完成这项工作所做的工作。 Thanks to @tom-ukelove for getting me headed in the right direction.感谢@tom-ukelove 让我朝着正确的方向前进。

I had the map, list and search filter on this page.我有 map,在此页面上列出和搜索过滤器。 The search filter is created with Search & Filter plugin.搜索过滤器是使用 Search & Filter 插件创建的。

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

In my Search & Filter settings, I set the results per page to 25, which is what I needed the list to do in loop #2.在我的搜索和过滤设置中,我将每页的结果设置为 25,这是我在循环 #2 中需要列表执行的操作。

To display all of the results on the map (loop #1), I used the following code to remove the 25 posts_per_page limit.为了在 map(循环 #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();
?>

Hope that helps someone in the future.希望对将来的人有所帮助。 Lmk if there's a more efficient way to do it. Lmk 如果有更有效的方法来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM