Site logo

Archived topic

Add Class to Categories

3 replies · Started by Math on July 29, 2020

Viewing posts 1–4 of 4

Hi,

On the Merch theme, under the Single Post Entry Header element, the following code is being run. Is there a way to add a class to the post categories which shows up above the title? I want to be able to add margins and stylize it.

<div id="custom-post-entry-header" class="custom-post-header grid-container">		
	<?php
	$categories = get_the_category();
	$separator = ' | ';
	$output = '';
	
	if ( ! empty( $categories ) ) {
		foreach( $categories as $category ) {
			$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
		}
		
		echo trim( $output, $separator );
	}
	
	the_title( '<h1 class="entry-title" itemprop="headline">', '</h1>' );

	if ( has_excerpt() ) : // Only show custom excerpts not autoexcerpts ?>
		<p class="entry-subtitle"><?php echo get_the_excerpt(); ?></p>
	<?php endif; ?>
</div>

Hi there,

edit the code and wrap a <div> around the code responsible for the categories:

?><div class="hero-categories"><?php

if ( ! empty( $categories ) ) {
	foreach( $categories as $category ) {
		$output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
	}
	
	echo trim( $output, $separator );
}

?></div><?php

Thank you so much!

You're welcome

This archived topic is closed to new replies.