-
Hi,
I’m trying to create unique 3 separate Content Template elements for pages/child-pages
COUNTRY
-STATE
–REGIONApart from individually adding each individual region/state/country manually in ‘Add Location rules’ for each of the 3 elements (which will be painful with 200+ pages), is there a better way to do it?
Thanks,
Pat -
Alvind
Hi there,
It’s not possible from the UI, but you can use the
generate_hook_element_display
filter.There is an example of usage in this article:
https://docs.generatepress.com/article/generate_hook_element_display/ -
Thanks Alvind
Tried this, but it doesn’t work.
For #1 under Display Rules – Location … this should be left blank ie ‘choose’?
—
#1 Block – Content Template [1574]
#3 Post ID – 1351
#2 Hook wp_body_open<?php add_filter( 'generate_hook_element_display', function( $display, $element_id ) { global $post; if ( 1574 === $element_id && is_page( 1351 ) ) { $display = true; } return $display; }, 10, 2 ); ?>
-
Hi there,
I need a little more info.
How do each of the 3 content templates relate to their pages ?For example in posts, it would be as simple as having Post Categories to set the location.
How does that compare to this requirement ? -
These are all pages (rather than posts)
We have country pages > state > city (state and city as child pages
Content Template 1 is for country pages
Content Template 2 for state pages
Content Template 3 for city pages -
OK. Tricky one.
We could build a function that determines if a page is a top level, a child, or a grandparent:function get_page_hierarchy_level($page_id = null) { if (is_null($page_id)) { $page_id = get_the_ID(); } $ancestors = get_post_ancestors($page_id); if (empty($ancestors)) { return 'top-level'; } if (count($ancestors) == 1) { return 'child'; } if (count($ancestors) == 2) { return 'grandchild'; } return null; }
Then you could use that function within the
generate_element_display
hook like so:add_filter( 'generate_hook_element_display', function( $display, $element_id ) { // get the page hierarchy level $level = get_page_hierarchy_level(); // check if top-level, child or grnadchild if ( 123 === $element_id && 'top-level' === $level || 456 === $element_id && 'child' === $level || 789 === $element_id && 'grandchild' === $level ) { $display = true; } return $display; }, 10, 2 );
Here we grab the
$level
and for each of the three element IDs ( update your element IDs ) we check its level and tell the element to display.This however will apply to all pages… so it may need some extra conditions to narrow that down
-
David – i have all the IDs of the pages and was planning to add them into each of the 3 hooks so that they call on the 3 Content Templates.
ie.
if ( 1574 === $element_id && is_page( 1351 ,1352,1353,etc,etc) ) {
$display = true;
}Would that work?
-
It would. If you don’t mind having a large array of numbers 🙂
-
I don’t mind manually getting all the IDs to put in. Hopefully there’s no significant performance impact due to the large array.
Unfortunately, couldn’t get it to work when following Alvin’s suggestion.
-
Can you show us the exact code you are using and wrap the code with the code tag?
Let me know 🙂
-
Hi Ying, this is what I’m using.
<?php add_filter( 'generate_hook_element_display', function( $display, $element_id ) { global $post; if ( 1574 === $element_id && is_page( 1351 ) ) { $display = true; } return $display; }, 10, 2 ); ?>
Tried this, but it doesn’t work.
For #1 under Display Rules – Location … this should be left blank ie ‘choose’?
—
#1 Block – Content Template [1574]
#3 Post ID – 1351
#2 Hook wp_body_open -
Try replacing
generate_hook_element_display
withgenerate_element_display
.For #1 under Display Rules – Location … this should be left blank ie ‘choose’?
Blank.
Not sure what you mean by hook:
wp_body_open
, if it’s a content template block element, then there isn’t a hook option. Let me know if I miss anything! -
Try replacing generate_hook_element_display with generate_element_display.
Done. Same result.
Not sure what you mean by hook: wp_body_open, if it’s a content template block element, then there isn’t a hook option. Let me know if I miss anything!
There are 2 elements.
1) a Block/Content-Template [id=1574]
2) a Hook with the code we’re discussing (where wp_body_open is under Settings/Hook. Tried a few others in the dropdown, but no luck).Let me know if I can clarify anything else.
-
Can you remove
global $post;
?And how did you add the code? Via Code snippets plugin or child theme
functions.php
? -
Can you remove global $post;?
Done. Same result.
And how did you add the code? Via Code snippets plugin or child theme functions.php?
It’s in the Hook element > edit Hook
-
Alvind
The snippet is not supposed to be added into the Hook Element; you need to use a Code Snippet plugin for that.
https://docs.generatepress.com/article/adding-php/
Then delete the Hook Element you used to add the snippet, as it’s no longer required. The filter to use is
generate_element_display
.
- You must be logged in to reply to this topic.