krista-m
2/14/2020 - 12:28 AM

Add category icons to single post

//* Add category icons to the entry header
add_action( 'genesis_entry_header', 'dk_add_category_icons' );
function dk_add_category_icons() {
		
	if ( is_single() ) {
	
		$post_id = get_the_ID();
		$categories = wp_get_object_terms( $post_id, 'category' );

		$html = '<div class="category-icon-wrapper">';

		foreach ( $categories as $cat ) {
			
			$html .= dk_get_category_html( $cat );
		}

		echo $html . '</div>';
	}
	
}

//* If a category icon has been assigned, construct the image HTML and return it
function dk_get_category_html( $category ) {
	
	$icon_url = get_field( 'category_icon', $category );
		
	if ( $icon_url ) {
		$link = get_category_link( $category );
		return '<a href="' . $link . '"><img src="' .  $icon_url . '" alt="' . $category->name . '" /></a>';
	}
	
	return '';
	
}
.single-post .entry-header .category-icon-wrapper {
	display: inline-block; 
	float: left;
}

.entry-header .category-icon-wrapper {
	display: inline-block; 
	float: left;
}

.entry-header.category-icons {
	margin-bottom: 20px;
}

.entry-header .category-icon-wrapper img {
	margin-bottom: 0;
	margin-right: 5px;
	width: 38px;
}