Question

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?

Était-ce utile?

La solution

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).)

Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top