php - 未定义变量:使用 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)我如何成功定义它以解决错误消息
非常感谢
菲尔
解决方案
所以,这里有几件事(基于最后的评论):
is_page()
不接受bool
值,它需要一个 id 或一个页面名称字符串(或任何一个的数组。$is_tree
当您传入pid
页面时被定义。该函数返回true
或false
。$is_tree
不在该函数之外使用 - 它只是布尔值的持有者。wpdocs_is_tree()
将单个帖子 ID 作为参数,但您传递的是一个数组。- 您正在分配
$pid
返回is_page()
true 或 false,而不是 post id,这wpdocs_is_tree()
是预期的。 - 我还更新了在第一行
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') ) { ?>
推荐阅读
- c# - 如何立即停止正在运行的 DbContext.SaveChanges()
- linux - 在 ELF LSB 共享对象模式下编译 curl
- python - 在python中的另一个带有条件的列表的帮助下从列表中提取值
- python - QMessageBox.warning 调用上的 QMessageBox 位置
- python - 不同平台上不同大小的窗口
- testng - 如果测试因 NoSuchElementException 或任何其他异常而失败,范围报告未将我的测试标记为失败
- python - 在 Python 中进行法语文本分析的最佳方法是什么?
- objective-c - 如何在 Visual Studio 中编译 Objective-C 代码
- sql - SQL 中 Not Like 运算符的替代方案
- javascript - ClojureScript 中 String.prototype.startsWith 的实现