Support » Themes and Templates » apply css class to the last list item for Categories

  • dhrubo

    (@dhrubo)


    hi,

    can i add a condition so it applies a css class to the last list item for categories for this loop wp_list_categories?

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • s_ha_dum

    (@apljdi)

    From what I can tell, you can’t do that, not without knowing your way around PHP pretty well. There isn’t a built in argument that will do what your want (again, so far as I can tell).

    You’d have to:
    1) Write a function that would, for example, preg_replace the last <li> in an unordered list. Then hook that function to ‘wp_list_categories’.
    2) Use javascript to insert the class. This is pretty easy to do using jQuery but will only work for javascript enabled browsers.
    3) Hack the wp_list_categories function– not especially advisable since it makes upgrading a pain and might break other things to boot.
    4) Copy ‘wp_list_categories’, paste it into your themes/functions.php file, rename it and edit it to do what you want.

    I’d lean toward the first option if you only want this to work in a particular theme. I’d lean toward #1 if you are building a plugin.

    Maybe someone brighter than I has a better idea.

    This seems dumb. The need is so common in design. Surely we can find an easier answer?

    Here created this solution when listing pages. Categories will be very similar…

    <!-- MAIN MENU -->
    <?php
    	function add_fl_class($input) {
    		if( !empty($input) ) {
    			$items= explode( "\n", $input );
    			$items[0]= str_replace("<li class=\"", "<li class=\"first ", $items[0]);
    			$items[count($items)-1]= str_replace("<li class=\"", "<li class=\"last ", $items[count($items)-1]);
    			$o= '';
    			foreach( $items as $i ) {
    				$o.= $i;
    			}
    		}
    		echo $o;
    	}
    	add_last_class( wp_list_pages('echo=0&depth=1&title_li=&orderby=id&show_images=0') );
    ?>
    <!-- END MAIN MENU -->

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘apply css class to the last list item for Categories’ is closed to new replies.