Archived topic
Child pages Hook
15 replies · Started by Matthew on July 12, 2021
I had this shortcode placed into a parent page
[wpb_childpages]
This worked to display a hyperlink list of the child pages.
But there were issues in the consistency of the display of the child pages, alphabetical listing on one parent page, but not on another despite checking and confirming the parent page and menu order attributes.
A better method I read is to place some PHP into the page template file.
<?php wpb_list_child_pages(); ?>
But rather than copying the code into the page template I try a Hook instead.
In the hook element I pasted the above PHP into the code area and used hook generate_after_main_content, Execute PHP checked on, Page > All Pages for display rule.
Usually when I do anything like this nothing happens on the front end of course, as with this time (due to my own ineptness)
Hi there,
Since you've mentioned page template file, I assume you've created a template for pages?
If that's the case, can you check if your custom page.php template file if it has do_action( 'generate_after_main_content' );?
Not having this means there's no generate_after_main_content hook to hook things in. :)
Hey yep the page.php template has generate_after_main_content.
I haven't created a template for the pages i.e. a copy of page.php and put it in my child theme or anything. I just wanted to make the hook for the child pages feature and apply it to my pages.
Thanks, it must be the function then.
Can you try changing <?php wpb_list_child_pages(); ?> to <?php echo wpb_list_child_pages(); ?>?
Let us know how it goes.
Hi Elvin it works now and I used the generate_before_main_content hook instead so it puts the child pages at the top like a table of contents.
It needs one improvement which is the consistency of the display of the child pages I mentioned in my original question. The child pages are not listed as they are in the admin, for example the Pages > All Pages have the following list:
Parent page
-Acrylic
-Canvas
-Fine Art
-Metal
-Photo Art
But on the front end (parent or child page) the listing goes:
-Canvas
-Metal
-Acrylic
-Fine Art
-Photo Art
I am assuming the admin is using an automatic alphabetical sorting so how can this be also used for the front end? Note that I have a separate matching set of pages for my FAQs for this setup, so my actual goal here is to make it even with this set (rather than strictly trying to get the admin the same as the front end); at the moment they are not the same
The child pages (in admin) all correctly have the parent page set to the same page, and all have the Order value as 0.
Can you share the code for the function wpb_list_child_pages() being used on the site?
Perhaps it'll give us an idea why it's being sorted that way. :D
I've had to lean away from the wpb function as it's caused some new issues.
For an alternative I am using the below code:
<?php
// Use wp_list_pages to display parent and all child pages all generations (a tree with parent).
$parent = 93;
$args = array( 'child_of' => $parent );
$pages = get_pages( $args );
if ( $pages ) {
$pageids = array();
foreach ( $pages as $page ) {
$pageids[] = $page->ID;
}
$args = array(
'title_li' => sprintf( __( 'Tree of Parent Page %s', 'textdomain' ), $parent ),
'include' => $parent . ',' . implode( ",", $pageids )
);
wp_list_pages( $args );
}
?>
Found at:
https://developer.wordpress.org/reference/functions/wp_list_pages/
So this is on the Hook element now. It seems to work however it's not changing the location of the link list for the child pages based on my selections from the hook itself. For example I try generate_after_entry_header and also generate_after_content (both actions from the content-page template) and the link list simply appears in the same spot near the bottom of the page. (It's above generate_after_content I know this much). It's clear there's some problem because I password protected a page to see, and the link list is displaying on the page before the password is entered whereas it should be hidden along with all page content.
Is the hook suitable for this code usage? Or should I place it straight into the page template. If it doesn't matter I'd probably rather use the GP hook.
In terms of my child theme the only other things I did was copy page.php and content-page.php into my child theme folder, that's it. I made no other changes to these two files. Is this correct?
There's definitely something strange about this.
Doing <?php echo wp_list_pages( $args ); ?> on a specific hook should render it on that hook area. Any chance you can setup a staging site clone and let us have backend access to it to check?
As for the sorting/order, you can refer to the documentation https://developer.wordpress.org/reference/functions/wp_list_pages/
Basing from the reference, I believe the arg to add it is sort_column and sort_order.
Hi Elvin,
I just added a guest user login for you in the private field.
You could refer to Pages > All Pages and the About us page has some child pages. You can see these on the front end in a bullet list following the glossary's Related terms.
The GP Hook element is called Child pages list, currently set to generate_after_entry_header.
Thanks.
I'm not sure the code on the Hook element is 100% correct. If we do a var_dump of the pageids and the parent, something should display but it doesn't seem to be working.
As a test - I've temporarily placed this code - https://share.getcloudapp.com/E0uAvbRP - on a test hook for the sake of comparison w/ a working wp_list_pages and a non-working one. (I've removed this after taking screenshots)
It displays this - https://share.getcloudapp.com/P8u9qEDZ and this - https://share.getcloudapp.com/yAurq2nA - on about page.
which narrows down the issue to the actual code placed on "Child pages list" Hook Element.
I've browsed through the list of pages and I didn't find any page w/ the id of 93. Perhaps that's the issue? Can you specify which page is the supposed parent?
What do you think $parent = 93; is, can I take it out of the code snippet?
The code is supposed to work (or I want it to) as a global type of thing for ANY pages that have parent/child relationships. So I don't understand why there's a reference to particular page.
Here's an attempt to simplify/explain the code:
Interpreting the code, it seems the intended purpose was to list the child pages of page with id of 93 and fetch the page IDs its children and then use that as arg for wp_list_pages( $args ).
I think if you want it to list the child pages of the current page, you change the value of $parent to get_queried_object_id().
Hi Elvin,
I only had to use this wp_list_pages snippet directly in the template, placed in the entry-content div
<?php
// https://developer.wordpress.org/reference/functions/wp_list_pages/
wp_list_pages( array(
'title_li' => '',
'child_of' => $post->ID,
'show_date' => 'modified',
'date_format' => $date_format
) );
?>
The hook was the problem, but it works correctly inside the template.
The code is significantly different.
That one inside the template uses 'child_of' => $post->ID, which is dynamic, meaning it gets the post id of the current post/page visited.
While the code on thehook with the variable $parent = 93; is basically 'child_of' => '93',.
It won't output anything if 93 has no child because there's no page ids to implode.
If I may suggest, I'd just copy that working code to the hook element and modify the 'child_of' => $post->ID, to 'child_of' => get_the_ID(),
That's right, the other lengthier code with the $parent = 93 I only originally resorted to because at the time (using the hook) it worked the closest to what I needed, which is the dynamic behavior you mentioned by getting the current post (and not a list of all pages). But it still didn't work well.
As it turns out, the other code (in my last post) when used within the page template it does what I thought it should and wanted it to, which is the dynamic child pages link list. We can now disregard the $parent = 93 snippet.
So you're saying if I wanted to still use the hook, I can take the code into the hook as below:
<?php
// https://developer.wordpress.org/reference/functions/wp_list_pages/
wp_list_pages( array(
'title_li' => '',
'child_of' => get_the_ID(),
'show_date' => 'modified',
'date_format' => $date_format
) );
?>
Or I can just keep it in the page template as per my past post