0

I'm using wp_list_pages() like so:

<?php wp_list_pages('title_li=&include=1714'); ?>

But would like it so that the link has a current_page_parent class under the following conditions:

if (is_singular('work')) {

Is this achievable with this function?

1 Answer 1

0

By default wp_list_pages() uses the Walker_Page class to create the HTML list, and there's a filter in there—page_css_class—for the CSS classes.

You should be able to do something like this:

/**
 * Filters the CSS classes.
 *
 * @param  array   $css_class    The current CSS classes. Array of strings.
 * @param  WP_Post $page         The current page object.
 * @param  int     $depth        Depth of the page.
 * @param  array   $args         Array of arguments.
 * @param  int     $current_page ID of the current page.
 * @return array                 The filtered array of classes.
 */
function wpse_378686_page_classes( $css_class, $page, $depth, $args, $current_page ) {
    if ( is_singular( 'work' ) && 1703 == $page->ID ) {
        $css_class[] = 'current_page_parent';
    }
    return $css_class;
}
add_filter( 'page_css_class', 'wpse_378686_page_classes', 10, 5 );

Add that code snippet to your active theme's functions.php file, or to a plugin that you would then activate.

Notes

  • is_singular( 'work' ) should be true on any single work custom post.
  • I've changed the code to use $page->ID instead of $current_page, as I think the $page variable contains the page that the page list is currently processing.

(Edited to reflect the comment re: page ID 1714. Edited again after clarification in the comments (and a change to page ID 1703 instead of 1714).)

9
  • thank you! although I should have been more specific sorry, I would like it so that it only applies to page 1714 and no others... as I am using the wp_list_pages function elsewhere in my theme for other pages
    – user39214
    Nov 23, 2020 at 18:40
  • 1
    Since $current_page is passed to the filter, you can easily check to make sure you're on page 1714 as well. (I've updated my code to do this, assuming that 1714 is the page's ID and not the title.)
    – Pat J
    Nov 23, 2020 at 18:56
  • thanks again! I'm actually trying to do it with a different page now like so if ( is_singular( 'work' ) && 1703 == $current_page — but it only seems to be applying the current_page_parent class to page 1714 list item! do I need to change the ID somewhere else too?
    – user39214
    Nov 23, 2020 at 19:34
  • 1
    Do you want the class to appear only on the nav menu item with ID 1714 (or 1703), or only when you're looking at the menu as it's presented on page 1714|1703?
    – Pat J
    Nov 23, 2020 at 21:08
  • 1
    Check the updated version of the code. I've switched to using $page->ID because that should reflect the item being processed in the list of pages.
    – Pat J
    Nov 24, 2020 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.