条件单独工作但不作为 OR

Condition works separately but not as OR

我有两个条件语句如下:

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

我想更改两者的最后一个条件,这将是简单的英语:

如果所有条件都匹配并且 URL 包含 '/blog/page/''/blog/tag/ '做点什么。

当我将最后一个条件从 '/blog/page/' 交换为 '/blog/tag/' 时代码有效。一旦我尝试同时拥有两者,代码就不再有效了。

我尝试将 && 更改为 and 并使用 || 作为 or 条件,以保持正确的优先级。我试图将它们放在括号之间以处理优先级,none 其中有效。

我什至试过了:

strpos($_SERVER['REQUEST_URI'], '/blog/page/') || strpos($_SERVER['REQUEST_URI'], '/blog/tag/') === false

这也没有帮助。

<?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/'))) {
    ...
}