我到处搜索解决方案并尝试了不同的解决方案,包括彻底解释的解决方案 here由 Chip Bennett 编写,但我似乎仍然无法正常工作。
结果的第一页工作正常,但从第 2 页开始它只显示索引模板并且仍然说找不到页面。这是我的代码:
Functions.php
function advanced_search_query($query) {
if ($query->is_search) {
$query->set('s', $_GET['s']);
$query->set('post_type', array( 'properties' ));
$query->set('meta_key', $_GET['meta_key']);
$query->set('orderby', $_GET['sortby']);
$query->set('order', $_GET['order']);
$query->set('posts_per_page', '5');
$query->set('paged', $paged);
if (isset($_GET['author'])) {
$query->set('author', $_GET['author']);
}
if (isset($_GET['propertytype'])) {
$query->set('taxonomy', 'propertytype');
$query->set('terms', $_GET['propertytype']);
}
$minCost = $_GET['minCost'];
$minCost = preg_replace("/[^0-9]/","", $minCost);
if ($minCost == ""){
$minCost = "0";
}
$maxCost = $_GET['maxCost'];
$maxCost = preg_replace("/[^0-9]/","", $maxCost);
if ($maxCost == ""){
$maxCost = "99999999999999";
}
$query->set('meta_query', array(
'relation' => 'AND',
array(
'key' => 'ce_location',
'value' => $_GET['location'],
'compare' => 'LIKE',
'type' => 'CHAR'
),
array(
'key' => 'ce_cost',
'value' => array($minCost, $maxCost),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'ce_bedrooms',
'value' => array($_GET['minBedrooms'], $_GET['maxBedrooms']),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
),
array(
'key' => 'ce_tenancy',
'value' => $_GET['tenancy'],
'compare' => 'LIKE',
'type' => 'CHAR'
)
));
};
return $query;
};
add_filter('pre_get_posts', 'advanced_search_query', 1000);
提取查询参数的代码
global $query_string;
$query_args = explode("&", $query_string);
$search_query = array();
foreach($query_args as $key => $string) {
$query_split = explode("=", $string);
$search_query[$query_split[0]] = urldecode($query_split[1]);
}
$search_query['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$search = new WP_Query($search_query);
循环代码:
if ( have_posts() ) : while (have_posts()) : the_post();
//loop content
endwhile;
if(function_exists('wp_simple_pagination')) { wp_simple_pagination(); } ?>
else :
echo 'Sorry, there are currently no property listings available';
endif;
任何建议将不胜感激。
编辑:
当我尝试访问第 2 页时,我还注意到 URL 被修改了。
这是第 1 页的网址:
http://localhost/cunningham/?location=&propertytype=&minBedrooms=1&maxBedrooms=9&minCost=0&maxCost=100000&meta_key=&tenancy=&s=
第 2 页网址:
http://localhost/cunningham/page/2/?location&propertytype&minBedrooms=1&maxBedrooms=9&minCost=0&maxCost=100000&meta_key&tenancy&s/
最佳答案
您的主要查询和次要(自定义)查询之间存在冲突。
你做事一半错一半对:-)。放过你正在做的事情
代码的第一部分是更改主查询的正确方法。实际上,这就是使事情正常进行所需的全部。不过你这里有几个缺陷
pre_get_posts 会改变所有 查询,主要和次要查询都类似。这不仅发生在前端,后端也会受到影响。您将只需要将更改应用到前端并且只针对主查询
function advanced_search_query($query) {
if ( !is_admin() && $query->is_main_query() && $query->is_search() ) {
//REST OF YOUR CODE
您不需要在pre_get_posts 中设置paged 参数。这是由主查询设置的,不需要更改,因此请将其删除。
除此之外,它应该可以正常工作。我无法测试您的代码,因为我没有与您相同的设置
您不需要自定义(辅助)查询。您已经修改了主查询以处理您的更改
您的循环很好。这就是您在 search.php 模板中所需要的,没有别的了
编辑
来自OP的评论
I'm pretty much assuming that there is a permalink issue somewhere. I don't have anything in my
.htaccessbesides the basics and my permalink structure is/%category%/%postname%/and with that structure pagination works all fine on another page that has a normal query hardcoded into the php file. It is literally only this search page having issues and it's all because paged isn't being set it the URL
解决方案
Well, I found the solution. The trailing slash was messing everything up
关于php - Wordpress:搜索结果分页仍然不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25944940/
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我使用Nokogiri(Rubygem)css搜索寻找某些在我的html里面。看起来Nokogiri的css搜索不喜欢正则表达式。我想切换到Nokogiri的xpath搜索,因为这似乎支持搜索字符串中的正则表达式。如何在xpath搜索中实现下面提到的(伪)css搜索?require'rubygems'require'nokogiri'value=Nokogiri::HTML.parse(ABBlaCD3"HTML_END#my_blockisgivenmy_bl="1"#my_eqcorrespondstothisregexmy_eq="\/[0-9]+\/"#FIXMEThefoll
导读语言模型给我们的生产生活带来了极大便利,但同时不少人也利用他们从事作弊工作。如何规避这些难辨真伪的文字所产生的负面影响也成为一大难题。在3月9日智源Live第33期活动「DetectGPT:判断文本是否为机器生成的工具」中,主讲人Eric为我们讲解了DetectGPT工作背后的思路——一种基于概率曲率检测的用于检测模型生成文本的工具,它可以帮助我们更好地分辨文章的来源和可信度,对保护信息真实、防止欺诈等方面具有重要意义。本次报告主要围绕其功能,实现和效果等展开。(文末点击“阅读原文”,查看活动回放。)Ericmitchell斯坦福大学计算机系四年级博士生,由ChelseaFinn和Chri
寻找有用的ruby的好网站是什么? 最佳答案 AgileWebDevelopment列出插件(虽然不是rubygems,我不确定为什么),并允许人们对它们进行评级。RubyToolbox按类别列出gem并比较它们的受欢迎程度。Rubygems有一个搜索框。StackOverflow对最有用的rails插件和rubygems有疑问。 关于ruby-如何搜索有用的ruby,我们在StackOverflow上找到一个类似的问题: https://stacko
我有很多这样的文档:foo_1foo_2foo_3bar_1foo_4...我想通过获取foo_[X]的所有实例并将它们中的每一个替换为foo_[X+1]来转换它们。在这个例子中:foo_2foo_3foo_4bar_1foo_5...我可以用gsub和一个block来做到这一点吗?如果不是,最干净的方法是什么?我真的在寻找一个优雅的解决方案,因为我总是可以暴力破解它,但我觉得有一些正则表达式技巧值得学习。 最佳答案 我(完全)不懂Ruby,但类似这样的东西应该可以工作:"foo_1foo_2".gsub(/(foo_)(\d+)/
我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o
我读了"BingSearchAPI-QuickStart"但我不知道如何在Ruby中发出这个http请求(Weary)如何在Ruby中翻译“Stream_context_create()”?这是什么意思?"BingSearchAPI-QuickStart"我想使用RubySDK,但我发现那些已被弃用前(Rbing)https://github.com/mikedemers/rbing您知道Bing搜索API的最新包装器(仅限Web的结果)吗? 最佳答案 好吧,经过一个小时的挫折,我想出了一个办法来做到这一点。这段代码很糟糕,因为它是
给定一个元素和一个数组,Ruby#index方法返回元素在数组中的位置。我使用二进制搜索实现了我自己的索引方法,期望我的方法会优于内置方法。令我惊讶的是,内置的在实验中的运行速度大约是我的三倍。有Rubyist知道原因吗? 最佳答案 内置#indexisnotabinarysearch,这只是一个简单的迭代搜索。但是,它是用C而不是Ruby实现的,因此自然可以快几个数量级。 关于Ruby#index方法VS二进制搜索,我们在StackOverflow上找到一个类似的问题:
据我们所知,Jekyll默认分页仅支持index.html,我想创建blog.html并在那里包含分页。有什么解决办法吗? 最佳答案 如果您创建一个名为/blog的目录并在其中放置一个index.html文件,那么您可以向_config.yml表示paginate_path:"blog/page:num"。不是使用根文件夹中的默认index.html作为分页器模板,而是使用/blog/index.html。分页器将根据需要生成类似/blog/page2/和/blog/page3/的页面。这将使您到达yourwebsite.com/b
这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw