我有两个条件语句如下:
if( isset( $query_string['page'] ) && strpos($_SERVER['REQUEST_URI'], '/blog/') !== false && strpos($_SERVER['REQUEST_URI'], '/blog/page/') === false ) {
if( $query->is_main_query() && !$query->is_feed() && !is_admin() && strpos($_SERVER['REQUEST_URI'], '/blog/') !== false && strpos($_SERVER['REQUEST_URI'], '/blog/page/') === false ) {
两个if语句中的最后一个条件是:
strpos($_SERVER['REQUEST_URI'], '/blog/page/') === false
我想改变两者的最后一个条件,用简单的英语:
如果所有条件都匹配且网址包含' / blog / page /' 或' / blog / tag /' 做点什么。
当我将最后一个条件从' / blog / page /' 交换到' / blog / tag /' 代码有效。一旦我尝试同时使用这两个代码,代码就再也无法工作了。
我已尝试将&&
更改为and
并使用||
作为条件,以保持正确的优先顺序。我试图将它们放在括号之间以便处理优先级,但没有一个工作。
我甚至尝试过:
strpos($_SERVER['REQUEST_URI'], '/blog/page/') || strpos($_SERVER['REQUEST_URI'], '/blog/tag/') === false
哪个也没有帮助。
答案 0 :(得分:1)
<?php
// Your code says "=== false" (doesn't match)
// but your English description says "contains either '/blog/page/' or '/blog/tag/'" (match)
// This assumes you want what your English description says
/**
* Returns a boolean indicating if the given URI part is found
*/
function match($uriPart)
{
return strpos($_SERVER['REQUEST_URI'], $uriPart) !== false;
}
/**
* Returns a boolean indicating if the given URI part is not found
*/
function doesNotMatch($uriPart)
{
return strpos($_SERVER['REQUEST_URI'], $uriPart) === false;
}
// In this case, "match('/blog/')" is redundant because you're checking for other strings which contain it.
// Nevertheless, I'm leaving it as-is.
if( isset( $query_string['page'] ) && match('/blog/') && (match('/blog/page/') || match('/blog/tag/'))) {
...
// In this case, "match('/blog/')" is redundant because you're checking for other strings which contain it.
// Nevertheless, I'm leaving it as-is.
if( $query->is_main_query() && !$query->is_feed() && !is_admin() && match('/blog/') && (match('/blog/page/') || match('/blog/tag/'))) {
...
}