Forums

Include post tags in body_class (2 posts)

  1. trmash
    Member
    Posted 7 months ago #

    Hi,

    I'm trying to figure out how to show an individual post's tags in the body_class.

    I can add the post category using

    function category_id_class($classes) {
    	global $post;
    	foreach((get_the_category($post->ID)) as $category)
    		$classes[] = $category->category_nicename;
    	return $classes;
    }

    but as yet have been unable to get the coding right to show the tags too.

    Any help much appreciated!

  2. trmash
    Member
    Posted 7 months ago #

    I seem to have solved my issue, but I'd still love some feedback from people more experienced in PHP. Here's how I got it to work.

    function category_id_class($classes) {
    	global $post;
    	$posttags = get_the_tags();
    	foreach((get_the_category($post->ID)) as $category)
    		$classes[] = $category->category_nicename;
    	if (is_single()) {
    	foreach ($posttags as $tag)
    		$classes[] = $tag->slug;
    	}
    	return $classes;
    }

Reply

You must log in to post.

About this Topic