[Resolved] Add Class to Categories

Home Forums Support Add Class to Categories

  • This topic has 3 replies, 2 voices, and was last updated 2 months ago by David.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1381313
    Math

    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>
    #1381476
    David
    Staff
    Customer Support

    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
    #1381728
    Math

    Thank you so much!

    #1381784
    David
    Staff
    Customer Support
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.