如何解决未定义的变量:使用Wordpress.org代码段时的is_tree
在键入此内容时,我被告知在Stackoverflow中有许多类似短语的问题及其答案-但是阅读了许多其他查询后,我仍然无法解决我似乎遇到的关于错误的问题有关未定义变量的消息。
说明。 。
我在子主题函数文件中使用代码片段来测试子页面,这些子页面是从以下Wordpress.org参考页获取的: (https://developer.wordpress.org/reference/functions/is_page/中的代码段4)。
我在代码下方添加了2行,以:A)定义$pid
是什么,以及B)一条if语句,该语句定义了我要调用的子页面(请参见下面的代码的最后两行):
* 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;
或$is_tree = true;
。 。 。但是显然不是这样!
如果有人可以看一下上面的代码并让我知道,我将非常感谢
A)为什么将变量视为未定义 和 B)我如何成功定义它以解决错误消息
非常感谢
菲尔
解决方法
因此,这里有几件事(根据最新评论):
-
is_page()
不接受bool
值,它需要一个id或一个页面名称字符串(或二者之一的数组)。
当您传入页面的 -
pid
。该函数返回true
或false
。$is_tree
不在该函数之外使用-它只是bool值的持有者。 -
wpdocs_is_tree()
将单个帖子ID作为参数,但是您正在传递数组。 - 您正在将
$pid
分配给is_page()
,这将返回true或false,而不是wpdocs_is_tree()
期望的发布ID。 - 我还更新了
wpdocs_is_tree()
以在第一行使用global $post
而不是$post = get_post()
,因为已经有一个全局变量。
$is_tree
时,将定义代码如下:
/* 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') ) { ?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。