inserting custom li class to wp_list_pages

I currently have a parent page with some child pages. I am able to list these child pages but would like to insert a custom li class. The wp_list_pages outputs <li class="page-item number"></li>. I would like for it to output <li class="hvr-underline"></li>. Here is the code I have so far:

$children = wp_list_pages( 'title_li=&child_of=".$post->ID."&echo=0' );

if ( $children) : ?>
    <ul class="menu ">
        <?php echo $children; ?>
    </ul>
<?php endif;

2 Answers
2

wp_list_pages allows you to include a walker function. That would give you complete freedom to add any classes you want, but it’s no a beginner’s method. You could start by reading this.

If you want a simple search and replace on the class, as your question seems to indicate, you can use the wp_list_pages filter like this:

add_filter ('wp_list_pages','wpse241119_replace_class',10,3);
function wpse241119_replace_class ($output, $r, $pages) {
  $output = str_replace ('page-item number', 'hvr-underline', $output);
  return $output;
  }

Leave a Comment