Support » Fixing WordPress » how do you add a style class to the last item on a list in wp_list_pages

  • Hi there!

    im having a problem on how to adjust or implement a css style class to the last item in wp_list_pages()

    I did manage to add a css style class in order to display a selected item in a list as follows:

    an extract from classes.php

    extract($args, EXTR_SKIP);
    $css_class = 'page_item page-item-'.$page->ID;
    if ( !empty($current_page) ) {
    $_current_page = get_page( $current_page );
    if ( in_array($page->ID, (array) $_current_page->ancestors) )
    $css_class .= ' current_page_ancestor';
    if ( $page->ID == $current_page )
    $css_class .= ' active'; <---- I CHANGED THIS TO ACTIVE
    elseif ( $_current_page && $page->ID == $_current_page->post_parent )
    $css_class .= ' current_page_parent';

    However my problem is I dont know how to add another class for the last item only. it should render something like as follows

    …<li class=”page_item page-item-1 ACTIVE”>Page 1…
    …<li class=”page_item page-item-2″>Page 2…
    …<li class=”page_item page-item-3″>Page 3…
    …<li class=”page_item page-item-4 LAST”>Page 4…

    So i dont know where in the wp code to add the class ‘last’

    I would be grateful for any help

    Thank you.


Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,

    You can do it with the a bit of code modification. You need to modify page.php file and check with wo_list to last posts and apply that style to that post with conditions.


    Shane G.

    Thanks Shane.

    do you mean ‘page.php’ within the theme folder? If so then can please advise me how to achieve this please?

    Thanks really appreciate it.



    i had the same probleme, then i found a function in a thread, but it did not work, so i rewrote it and is works pretty well,

    i’m shure it could be imporved by comdining both regex into one, but still, it works pretty good

    it will ad a first and a last class to the li

    function add_last_class($input) {
    	if( !empty($input) ) {
    		$pattern = '/<li class="/is';
    		$replacement = '<li class="first ';
    		$input = preg_replace($pattern, $replacement, $input);
    		$pattern = '/<li class="(?!.*<li class=")/is';
    		$replacement = '<li class="last ';
    		$input = preg_replace($pattern, $replacement, $input);
    		echo $input;
    /* the echo=0 parma is important here */

    hope it helps

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘how do you add a style class to the last item on a list in wp_list_pages’ is closed to new replies.