Archived topic
Display Rule for child pages
9 replies · Started by Regina on December 28, 2019
Hi there,
I created a hook with an arrow linked to the parent page, which I want to display on all child pages of one parent page, but not on the parent page itself. I miss the exact display rule like "all child pages" in combination with "parent page". Is it possible to create my own display rule?
I found the following – do I have to create two hooks and one with something like this?:
***
Another useful example is to set the hook to display on the parent page and all the child pages automatically:
add_filter( 'generate_hook_element_display', function( $display, $element_id ) {
global $post;
if ( 1180 === $element_id && ( is_page() && $post->post_parent == '415' ) ) {
$display = true;
}
return $display;
}, 10, 2 );
You need to replace 1180 with the ID of your Element, and 415 with the ID of the parent page.
***
Thank you so much in advance!
Hi there,
That one filter should do exactly what you're looking for.
All you need to do is:
1. Create the Hook, and note the Hook ID (it's in the URL when you save your hook).
2. Replace 1180 in the filter with the ID.
3. Replace 415 in the filter with the ID of the parent page you're targeting.
That should be it.
We're planning on adding a Child Page option in the Display Rules soon :)
Hi Tom,
and how do I find out the ID of my Element? Do I allocate it in the code of my element myself?
I choosed custom hook and also checked PHP in the hook withe the code. Is a location rule necessary? (Actually I have it in the Element hook.)
Do not plan too long to add this Child Page option in the Display Rules ;-)
Thanks again!
Sorry, you already wrote, that the ID is in the URL.
it´s .../post.php?post=1513&action=edit
I think the ID must be 1513 (?)
It still doesn´t work.
That's right - did you also update the page parent ID?
yes both.
I used the plugin code snippets now and it works. Maybe it was a misunderstanding using two hooks for that?
Ah yes - filters need to be added in your functions.php file or in Code Snippets :)
it is not working with CPT children, can you advise how to make it work?
Thanks a lot
Hi there,
this line:
if ( 1180 === $element_id && ( is_page() && $post->post_parent == '415' ) ) {
one of the conditions is_page() needs to be changed for is_singluar('your-cpt'):
https://developer.wordpress.org/reference/functions/is_singular/
+1 for pages rules based on parent page