• Resolved CaptainCrunch

    (@captaincrunch)


    Hey guys,

    I have a question that’s closely related to another topic I’ve posted a few days ago. The issue got solved, thanks to you guys. I’m using the Thematic Theme framework and am currently building a child theme based on that.

    link

    The issue is quite the same. But now I’d like to style the category tag that pop’s up below posts. Jan Dembowski helped me out with his code and it works like a charm. I’m now able to style each post tag as desired. I’d like to do the same with the category-tags. So say a post got published under the categories “Blog” and “Current affairs” I’d like that to show up below the post in seperate boxes instead of just text seperated by commas. Therefore I need to be able to style (and basically wrap) each category tag in a <div>

    See Jan’s code that I’ve put in my functions.php and that worked, below:

    add_filter( 'term_links-post_tag' , 'mh_add_wrap' );
    function mh_add_wrap( $c ){
            foreach( (array) $c as $k => $v ){
                    $a[] = '<span class="tags_style">' . $v . '</span>';
            }
            return $a;
    }

    …when I’m calling get_the_tag_list() it shows up like this:
    <span class="tags_style"><a href="https://. . ./tag/tag1/" rel="tag">Tag1</a></span>
    Which is exactly what I intended. I would be over the moon, if I could do the same thing with the category tags.

    I’m completely new to PHP so I was wondering, if a filter could be called, and if there’s a similar approach for the category tags available, which I managed to call via:
    get_the_category_list(' ');
    in my childthemes functions.php

    Cheers ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    I don’t think you can filter get_the_category_list, but you can use get_the_category() to mimic your output.
    Use this instead of get_the_category_list(' ');

    <?php
    	$categories = get_the_category();
    	$seperator = ' ';
    	$html = '';
    	if($categories){
    		foreach($categories as $category) {
    			$html .= '<span class="tags_style"><a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></span>'.$seperator;
    		}
    	echo trim($html, $seperator);
    	}
    ?>

    Thread Starter CaptainCrunch

    (@captaincrunch)

    Hey keesiemeijer,

    Thanks for your input. I guess I must have made some mistake while adding the code, because it now replaces the categories and shows ‘Array’ (just spimple text)…

    Just to be sure: I’ve put the above code in my functions.php and called it via get_the_category(); ?

    Thanks

    Moderator keesiemeijer

    (@keesiemeijer)

    Just to be sure: I’ve put the above code in my functions.php and called it via get_the_category(); ?

    No, replace <?php echo get_the_category_list(' '); ?> with the code above in your theme template files.

    If you rather have a function for this you can put this in your functions.php:

    function my_get_the_category_list($seperator = ' '){
    	global $post;
    
    	if (empty( $post->ID ) ) {
    			return false;
      }
    
    	$categories = get_the_category($post->ID);
    	$html = '';
    	if($categories){
    		foreach($categories as $category) {
    			$html .= '<span class="tags_style"><a href="'.get_category_link($category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a></span>'.$seperator;
    		}
    	return trim($html, $seperator);
    	}
    	return false;
    }

    and replace <?php echo get_the_category_list(' '); ?> with <?php echo my_get_the_category_list(' '); ?> in your theme template files

    Thread Starter CaptainCrunch

    (@captaincrunch)

    Hey there keesiemeijer,

    Awesooooooome!! ?? That did the trick! Brillant!! Thanks so much for your help. Your code solved my problem. Gosh, PHP really begins to be kinda fun :-))

    Thread Starter CaptainCrunch

    (@captaincrunch)

    Oh, and topic resolved — sorry, forgot to check the box in my previous post ??

    Moderator keesiemeijer

    (@keesiemeijer)

    So glad you got it resolved ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to style — get_the_category_list(' ') — using a css pseudo class?’ is closed to new replies.