• Hi there

    I need to add a style class item to the last wp_list_pages item

    I managed to add a style class for a selected item in the list like as below:

    `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 i dont know how to add it to last item. it should render html as follows

      <li class=”page_item page-item-2 active“>Page 1
      <li class=”page_item page-item-3″>Page 2
      <li class=”page_item page-item-4″>Page 3
      <li class=”page_item page-item-5 last“>Page 4

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

    I would be grateful for any help

    Thank you.

    Jim

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

    Don’t know if you ever found an answer, but for future readers, my plugin Classy WP List Pages will do just that.

    Quick note —

    Classy wp_list_pages will not add a class to the last page in a certain level of the list if that item has sub-pages.

    hey,

    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 */
    add_last_class(wp_list_categories('title_li=&echo=0'));

    hope it helps

    Hey Stakabo, just used your code and it rocks. Didn’t need the ‘first’ class listing so I simply removed it. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to add a style class to wp_list_pages last item’ is closed to new replies.