WordPress.org

Support

Support » How-To and Troubleshooting » Include post tags in body_class

Include post tags in body_class

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

Viewing 1 replies (of 1 total)
  • 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;
    }

Viewing 1 replies (of 1 total)
  • The topic ‘Include post tags in body_class’ is closed to new replies.