I would use a custom walker and then extend the Walker_Page::start_el()
method, like so:
// Based on Walker_Page for WordPress v6.0
// @link https://github.com/WordPress/wordpress-develop/blob/6.0/src/wp-includes/class-walker-page.php#L105-L219
class My_Walker_Page extends Walker_Page {
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
/* This checks if the current page in the list is the current page you're on.
* - $current_object_id is the ID of the current page you're on
* - $data_object->ID is the ID of the current page in the list
*/
$is_current_page = ( (int) $current_object_id === (int) $data_object->ID );
// This one adds the "read more" inside the link, i.e. before the </a> tag.
if ( $is_current_page ) {
$args['link_after'] .= ' READ MORE 1 (inside "a")';
}
// Call the parent method to build the (complete) <li ...><a ...>...</a> part.
parent::start_el( $output, $data_object, $depth, $args, $current_object_id );
// This one adds the "read more" outside the link, i.e. after the </a> tag,
// but still inside the same or current LI element.
if ( $is_current_page ) {
$output .= ' READ MORE 2 (outside "a")';
}
}
}
Things to note:
-
You could instead use a conditional tag such as
is_page( $data_object->ID )
oris_single( $data_object->ID )
, but using the above(int) $current_object_id === (int) $data_object->ID
is easier because it works regardless the post type. -
Just choose one of the
if ( $is_current_page ) { ... }
blocks above, or use both if you want to.. 🙂
And then pass an instance of the above class to wp_list_pages()
(via the walker
argument), e.g.
// NOTE: It's up to you on how to load the PHP FILE which defines the My_Walker_Page
// class, but for example, require_once __DIR__ . '/includes/class-my-walker-page.php';
wp_list_pages( array(
'walker' => new My_Walker_Page(),
) );
Additional Notes
-
Your
dkc_list_subpgm()
code could be simplified like so where I used just onewp_list_pages()
instead of two:function dkc_list_subpgm() { global $post; $child_of = $post->ID; if ( is_singular( 'pgm' ) && $post->post_parent ) { $child_of = $post->post_parent; } $childpages = wp_list_pages( array( 'post_type' => 'pgm', 'sort_column' => 'menu_order', 'title_li' => '', 'child_of' => $child_of, 'echo' => 0, // just use 0 (without quotes) and not '0' 'walker' => new My_Walker_Page(), ) ); if ( $childpages ) { return '<ul class="wpb_page_list">' . $childpages . '</ul>'; } return ''; }