[英]Undefined variable: is_tree when using Wordpress.org Code Snippet
在鍵入此內容時,我被告知在 Stackoverflow 中有許多類似措辭的問題以及對此的答案 - 但在閱讀了許多其他查詢后,我仍然沒有更接近解決我似乎遇到的關於一個錯誤消息的問題未定義的變量。
解釋。 . .
我在我的子主題函數文件中使用代碼片段來測試我從以下 Wordpress.org 參考頁面中獲取的子頁面:( https 中的片段 4://developer.Z1870A829D9BC69ABF5ZFE.org/functionF6 /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 行的變量 $is_tree (這是上面代碼中的最后一行<?php if (is_page($is_tree) &&,is_paged() &&?is_page('society-business','journal-archive')) {?>
) 未定義。
php 的這個區域超出了我目前的理解范圍 - 但對我來說$is_tree
變量定義為$is_tree = false;
或$is_tree = true;
. . . 但顯然事實並非如此!
如果有人可以查看上面的代碼並讓我知道,我將不勝感激
A)為什么變量被視為未定義和 B)我如何 go 關於成功定義它以解決錯誤消息
非常感謝
菲爾
所以,這里有幾件事(基於最后的評論):
is_page()
不接受bool
值,它需要一個 id 或一個頁面名稱字符串(或任何一個的數組。$is_tree
在您傳入頁面的pid
時被定義。 function 返回true
或false
。 $is_tree
不在 function 之外使用 - 它只是布爾值的持有者。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') ) { ?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.