While typing this I'm being told there are many similarly phrased questions with answers to this in Stackoverflow - but having read many of the other queries I'm still no nearer to solving the issue I seem to be having regarding an error message about an undefined variable.
To explain . . .
I'm using a code snippet in my child theme functions file to test for sub-Pages which I've taken from the following Wordpress.org reference page: (snippet 4 in https://developer.wordpress.org/reference/functions/is_page/).
I've added 2 lines below the code to A) define what $pid is and B) an if statement that defines which sub pages I want to call up (see last 2 lines in the code below):
* 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')) {?>
The code has worked OK for a while but the website is now becoming very slow. Having installed the 'Query Monitor' plugin I can see various php errors that I want to clear up/debug - and this is one of them.
The error message says that the variable $is_tree on line 579 (which is the last line in the above code <?php if (is_page($is_tree) && !is_paged() && !is_page('society-business','journal-archive')) {?>) is undefined.
This area of php is beyond my current understanding - but for me the $is_tree variable is defined as either $is_tree = false; or $is_tree = true; . . . but obviously this is not the case!
I'd very much appreciate if someone could look at the above code and let me know
A) why the variable is being seen as undefined and B) how I go about successfully defining it in order to resolve the error message
Many thanks
Phil
$is_treeis not defined because that variable is not in the GLOBAL scope. That variable only exists within that function. You can try thisis_page( wpdocs_is_tree( $pid ) )as that function will return TRUE or FALSE. - disinfor$is_treeis inside the function, it'll be undefined when used outside of it. However still don't get why it's undefined tbh - is it because it's boolean (true/false) - how do you define that? Meanwhile tried your suggestion of usingis_page( wpdocs_is_tree( $pid ) )in the line<?php if (is_page($is_tree) && !is_paged() && !is_page('society-business','journal-archive')) {?>but it's still sayingis_treeis undefined. Any ideas? - Phil Legg$is_treegets defined when you pass the$pidand any of theifstatements are met. - disinfor<?php if (is_page( wpdocs_is_tree( $pid ) ) && !is_paged() && !is_page('society-business','journal-archive')) {?>Is that not what you meant? - Phil Legg