WordPress.org

Ready to get started?Download WordPress

Forums

apply css class to the last list item for Categories (4 posts)

  1. dhrubo
    Member
    Posted 5 years ago #

    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

  2. s_ha_dum
    Member
    Posted 5 years ago #

    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.

  3. doublejosh
    Member
    Posted 5 years ago #

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

  4. doublejosh
    Member
    Posted 5 years ago #

    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 -->

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.