Support » Fixing WordPress » how to add class=”last” in ul li

  • hello can someone tell how i should add this code class="last" to here

    <li class="page-slug-about"<?php if(is_page('page')) echo ' class="active"'; ?>><a href="?page_id=31">contact</a></li>

    my pages are in <body class code so links in my menu needs to be written like <li class="page-slug-about" but i need to add class="last" code to my last link in the menu. as it’s not displayed allright.

    can someone help me to add class="last" in my menu link above???

Viewing 1 replies (of 1 total)
  • 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

Viewing 1 replies (of 1 total)
  • The topic ‘how to add class=”last” in ul li’ is closed to new replies.