【问题标题】:Undefined variable: is_tree when using Wordpress.org Code Snippet未定义变量:使用 Wordpress.org 代码片段时的 is_tree
【发布时间】:2020-08-16 23:23:21
【问题描述】:

在键入此内容时,有人告诉我,在 Stackoverflow 中有许多类似措辞的问题及其答案 - 但在阅读了许多其他查询后,我仍然没有更接近解决我似乎遇到的关于错误的问题关于未定义变量的消息。

解释一下。 . .

我在我的子主题函数文件中使用代码 sn-p 来测试我从以下 Wordpress.org 参考页面获取的子页面: (https://developer.wordpress.org/reference/functions/is_page/ 中的 sn-p 4)。

我在代码下方添加了 2 行 A) 定义 $pid 是什么和 B) 定义我想要调用的子页面的 if 语句(参见下面代码中的最后 2 行):

 * Check whether we are on this page or a sub page
 *
 * @param int $pid Page ID to check against.
 * @return bool True if we are on this page or a sub page of this page.
 */
function wpdocs_is_tree( $pid ) {      // $pid = The ID of the page we're looking for pages underneath
    $post = get_post();               // load details about this page
 
    $is_tree = false;
    if ( is_page( $pid ) ) {
        $is_tree = true;            // we're at the page or at a sub page
    }
 
    $anc = get_post_ancestors( $post->ID );
    foreach ( $anc as $ancestor ) {
        if ( is_page() && $ancestor == $pid ) {
            $is_tree = true;
        }
    }
    return $is_tree;  // we arn't at the page, and the page is not an ancestor
}
?>

<?php $pid = is_page(array('my-newcomen','the-piston-engine-revolution')) ?>
<?php if (is_page($is_tree) && !is_paged() && !is_page('society-business','journal-archive')) {?> 

代码运行了一段时间,但网站现在变得非常缓慢。安装了“查询监视器”插件后,我可以看到我想要清除/调试的各种 php 错误——这就是其中之一。

错误信息表明第 579 行的变量 $is_tree(即上述代码 &lt;?php if (is_page($is_tree) &amp;&amp; !is_paged() &amp;&amp; !is_page('society-business','journal-archive')) {?&gt; 中的最后一行)未定义。

这个 php 领域超出了我目前的理解范围 - 但对我来说,$is_tree 变量 被定义$is_tree = false;$is_tree = true; 。 . .但显然情况并非如此!

如果有人可以查看上面的代码并告诉我,我将不胜感激

A) 为什么变量被视为未定义 和 B)我如何成功定义它以解决错误消息

非常感谢

菲尔

【问题讨论】:

  • $is_tree 未定义,因为该变量不在全球范围内。该变量仅存在于该函数中。您可以试试这个is_page( wpdocs_is_tree( $pid ) ),因为该函数将返回 TRUE 或 FALSE。
  • 一直在阅读有关变量作用域的信息,所以现在了解因为$is_tree 在函数内部,所以在函数外部使用时它将是未定义的。但是仍然不明白为什么它是未定义的 tbh - 是因为它是布尔值(真/假) - 你如何定义它?同时尝试了您在&lt;?php if (is_page($is_tree) &amp;&amp; !is_paged() &amp;&amp; !is_page('society-business','journal-archive')) {?&gt; 行中使用is_page( wpdocs_is_tree( $pid ) ) 的建议,但它仍然说is_tree 未定义。有什么想法吗?
  • 你是如何使用它的?发布您尝试过的确切行。当您传递$pid 并且满足任何if 语句时,$is_tree 被定义。
  • 我在原始问题&lt;?php if (is_page( wpdocs_is_tree( $pid ) ) &amp;&amp; !is_paged() &amp;&amp; !is_page('society-business','journal-archive')) {?&gt; 中发布的代码中将其用作最后一行,这不是您的意思吗?

标签: php wordpress variables undefined


【解决方案1】:

所以,这里有几件事(基于最后一条评论):

  1. is_page() 不接受 bool 值,它需要一个 id 或一个页面名称字符串(或两者的数组。
  2. $is_tree 在您传入页面的pid 时被定义。该函数返回truefalse$is_tree 不在该函数之外使用 - 它只是布尔值的持有者。
  3. wpdocs_is_tree() 将单个帖子 ID 作为参数,但您传递的是一个数组。
  4. 您将$pid 分配给is_page(),这将返回一个真或假,而不是一个帖子ID,这是wpdocs_is_tree() 所期望的。
  5. 我还更新了wpdocs_is_tree() 以在第一行使用global $post 而不是$post = get_post(),因为已经有一个可用的全局变量。

代码应该是这样的:

/* Check whether we are on this page or a sub page
*
* @param int $pid Page ID to check against.
* @return bool True if we are on this page or a sub page of this page.
*/
function wpdocs_is_tree( $pid ) {      // $pid = The ID of the page we're looking for pages underneath
    global $post;               // load details about this page

    $is_tree = FALSE;

    if ( is_page( $pid ) ) {
        $is_tree = TRUE;            // we're at the page or at a sub page
    }

    $anc = get_post_ancestors( $post->ID );
    foreach ( $anc as $ancestor ) {
        if ( is_page() && $ancestor === $pid ) {
            $is_tree = TRUE;
        }
    }

    return $is_tree;  // we aren't at the page, and the page is not an ancestor
}

?>

<?php
/* In plain english (in order): if the current page is my-newcomen or the-piston-engine-revolution 
 * AND wpdocs_is_tree returns true by passing the current page id, then continue 
 * AND is NOT society-business or journal-archive
 */
if ( is_page( ['my-newcomen', 'the-piston-engine-revolution'] ) && wpdocs_is_tree( get_queried_object_id() ) && ! is_page('society-business','journal-archive') ) { ?> 

【讨论】:

  • 感谢您在上一条评论开头的个别解释-很好-我一直在学习。但是,当我插入您的代码时,它仍然无法正常工作!试图找出错误是什么?基本上原始代码在说。 . .如果页面是 my-newcomen 或 the-piston-engine-revolution - 或它们的子页面/子页面。但也不是社会-商业-或-期刊-档案-然后继续。你的最后一行是这样说的吗? (因为我看不到对&amp;&amp; !is_page('society-business','journal-archive') 的任何引用) - 关于为什么它还没有工作的任何想法?
  • 没有理由检查它是否不是另外两个页面,因为第一个is_page( [my-newcom..... 如果是则返回true,否则返回false。那些其他页面是my-newcomenthe-piston... 的子页面吗?如果是这样,那么我们将需要这些检查。另外,既然我看到了您实际上要做什么,我将修改答案-因为它不会捕获子页面。
  • 是的,social-business 和 journal-archive 都是 my-newcomen 的子项,因此为什么要列出该条件(抱歉之前应该更清楚),您将如何修改答案?
  • 我已经更新了答案。因此,基本上,如果所有这些语句都是 TRUE,if 语句将继续 - 是页面 my-newcomenthe-piston... 并且 wpdocs_is_tree 返回 TRUE 并且它不是其他任何一个页面。
  • 感谢您的回答,但仍然不高兴!基本上我们在这个时间点的立场是。 . . if ( is_page( ['my-newcomen', 'the-piston-engine-revolution'] ) &amp;&amp; wpdocs_is_tree( get_queried_object_id() ) &amp;&amp; ! is_page('society-business','journal-archive') ) { ?&gt; 不起作用 - 而 &lt;?php $pid = is_page(array('my-newcomen','the-piston-engine-revolution')) ?&gt; &lt;?php if (is_page($is_tree) &amp;&amp; !is_paged() &amp;&amp; !is_page('society-business','journal-archive')) {?&gt; 起作用 - 但会产生 $is_tree 未定义的警告。任何想法为什么以及如何解决这个问题?
猜你喜欢
  • 2023-01-13
  • 2021-03-29
  • 2019-12-31
  • 1970-01-01
  • 2015-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
相关资源
最近更新 更多