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?
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?
Share Improve this question edited Nov 23, 2020 at 17:56 Pat J 12.4k2 gold badges28 silver badges36 bronze badges asked Nov 23, 2020 at 17:35 user39214user392141 Answer
Reset to default 0By 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 betrue
on any singlework
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).)