At the moment we can check if the pages has a parent or if the page is a child of another page.
I need more specific rules. I’d like to show a field group only if the page has a specific amount of ancestors (for example if the page is 3rd level page in the pages hierarchy).
It would be nice if we could specify the following rules:
– Show the field group if the page has at least X ancestors
– Show the field group if the page has at most X ancestors
– Show the field group if the page is X-level page
It should be quite simple to implement. WordPress has a buil-in function to get post depth:
This is a custom filter that does that
Thanks. This is exactly what I’m looking for.
But it would be good if such a feature was available out of the box.
I found a little bug in the example you’ve provided. There’s a typo:
isset($option['post_id'] instead of
// Edit 2;
I’ve found another bug.
$page_level doesn’t return a correct value.
The logic is overcomplicated. It could be simplified:
$page_level = count(get_post_ancestors($options['post_id'])) + 1; works fine.
One more comment:
There’s one disadvantage of this solution.
acf/location/rule_operators filter doesn’t allow to set operators for a specific rule. It’s a general filter that applies for all the rules. But as far I know there’s no other option at the moment.
I fixed the first bug thanks, I added that later when I realized by making that change it could be used for any hierarchical post type and needed to make sure the post_id was set to get the post type.
But, for you’re second one, if you change that then you may have trouble and there is a reason that I do get ancestors on the parent id rather than the post id. When you change the parent of a page this does not become effective until after the post is saved. The AJAX call includes the new parent. Get ancestors on the post ID in the AJAX call can still return the same list of ancestors even if the depth has not changed. And while I could combine that into a single line it is more clear what is being done with it on two lines. Code clarity is more important to me than code brevity.
Yes, it might be nice to have this is the core of ACF, I can’t do that. I also know that the developer like to keep ACF to what will be used by the majority, among other things. While this could be useful for some I don’t know if it would be useful to most people. That’s why he’s made sure there are plenty of hooks so that we can make modifications.
The topic ‘More specific location rules: nth-level page.’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!