So I created a page template that has a sidebar with a menu. I want the menu to be conditional based on the parent page. I was able to use the Parent Page conditions for the immediate child of a particular parent page, but of course I ran into the issue of pages that are further down the parent/child relationship structure. So Pages that are grandchildren of a Parent Page.
I saw that you can use a php function in the dynamic data for conditions, but every function I have tried in order to check the current page if it is the ancestor of a root parent page doesn’t seem to work. I am very likely not understanding the functions themselves.
How would I make an element conditional based on the root page ancestor or “grandparent”, if that’s even the correct term?
I have this issue as well, and had it working in Oxygen builder, but am having trouble getting it to work in Bricks. I have multiple menus that I want to show in a sidebar based on the top-level page ancestor in the same way that you asked about.
My code returns the proper page ancestor ID, but using it in a condition has not worked. If anyone sees this and can point me in the right direction, I would be grateful.
Here’s my condition:

And my code:
<?php
function sa_get_page_root() {
global $post;
if ($post->post_parent) {
$ancestors=get_post_ancestors($post->ID);
$root=count($ancestors)-1;
$parent = $ancestors[$root];
} else {
$parent = $post->ID;
}
return $parent;
}
Update: Since Bricks has become more strict about echoing php in templates, I had to add my custom function as per the Code Review section. I added the following to my functions.php, and it works as expected now:
add_filter( 'bricks/code/echo_function_names', function() {
return [
'sa_get_page_root',
];
} );
1 Like