首页 > 解决方案 > 未定义变量:使用 Wordpress.org 代码片段时的 is_tree

问题描述

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

解释 。. .

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

我在代码下方添加了 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 行(上面代码中的最后一行<?php if (is_page($is_tree) && !is_paged() && !is_page('society-business','journal-archive')) {?>)上的变量 $is_tree 未定义。

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

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

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

非常感谢

菲尔

标签: phpwordpressvariablesundefined

解决方案


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

  1. is_page()不接受bool值,它需要一个 id 或一个页面名称字符串(或任何一个的数组。
  2. $is_tree当您传入pid页面时被定义。该函数返回truefalse$is_tree不在该函数之外使用 - 它只是布尔值的持有者。
  3. wpdocs_is_tree()将单个帖子 ID 作为参数,但您传递的是一个数组。
  4. 您正在分配$pid返回is_page()true 或 false,而不是 post 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') ) { ?> 

推荐阅读