• Resolved lordsina

    (@lordsina)


    Hello all..
    i want to add two images before and after any category in my category list in side bar.i found out that wp_list_categories() dosnt have before and after arg in wordpress 2.8 , so how can i do that?
    i read a topic here and found this query :

    <?php
    $categories = get_categories();
    foreach ($categories as $cat) {
    echo ‘

    • category_nicename.’/”><span>’.$cat->cat_name.'</span>
    • ‘;
      }
      ?>

      its good because of ability of using <span> so it can be replace with img

      but the problem is that the link to the category seems not to work correctly ( it has problem with my permalink structre )

      so what is your suggestion? can u give me a correct query? i spent our for this but found nothing 🙁

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter lordsina

    (@lordsina)

    sorry the above code is this :

    <?php
      $categories = get_categories();
      foreach ($categories as $cat) {
      echo '<li><a href="'.get_option('home').'/'.get_option('category_base').'/'.$cat->category_nicename.'/"><span>'.$cat->cat_name.'</span></a></li>';
      }
    ?>

    [moderated–bump removed. Please refrain from bumping as per Forum Rules]

    have you tried using get_category_link?

    <?php
      $categories = get_categories();
      foreach ($categories as $cat) {
      echo '<li><a href="'.get_category_link( $cat->cat_ID ).'/"><span>'.$cat->cat_name.'</span></a></li>';
      }
    ?>
    Thread Starter lordsina

    (@lordsina)

    thanx man , it works , u made my day , but can u please tell me how can have the category count after the name in this code?

    you should be able to do $cat->category_count to get the count. see the get_categories examples or the category member variables for more information.

    e.g.

    <?php
      $categories = get_categories();
      foreach ($categories as $cat) {
      echo '<li><a href="'.get_category_link( $cat->cat_ID ).'/"><span>'.$cat->cat_name.' ('. $cat->category_count .')'</span></a></li>';
      }
    ?>
    Thread Starter lordsina

    (@lordsina)

    this is exactly what i wanted..really thanx

    Great! Just what I was searching for. But for an added layer of complexity, how do you make this work with the Show Active Category plugin? Here’s the original code for this plugin:

    function show_active_category($text) {
    
    	global $post;
    
    	if( is_single() || is_category() ) {
    
    		$categories = wp_get_post_categories($post->ID);
    
    		foreach( $categories as $catid ) {
    			$cat = get_category($catid);
    			if(preg_match('#>' . $cat->name . '</a>#', $text)) {
    				$text = str_replace('>' . $cat->name . '</a>', ' class="active-cat">' . $cat->name . '</a>', $text);
    			}
    		}
    
    	}
    
    	return $text;
    
    }
    
    add_filter('wp_list_categories', 'show_active_category');

    All this does is add a “active-cat” class to the anchor tag relevant to the single page’s category. As it stands it doesn’t work. I also tried changing the $categories variable names so they don’t conflict, and changing the filtered function to that of get_categories(), since this is the actual function used to list the categories, not wp_list_categories().

    Any englightenment would be very helpful.

    thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘wp_list_categories() with img before and after’ is closed to new replies.