Amy-Mir / inc / categories-icons.php
categories-icons.php
Raw
<?php
/**
 * Custom functions that is dependent of the theme styles
 *
 * Transform core categories into icons
 *
 */

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;

/**
 * Processes post categories
 */
add_action( 'after_setup_theme', 'process_post_cat' ); 

/**
 * process_post_cat
 * @param int $post_id | use get_the_ID while in the Loop to retieve post IDs
 * @param bool $include_text | true to show category name as text on the icon side else only icon is shown
 */
function process_post_cat($post_id, $include_text = false) {
    $output = "";   
    $post_category = get_the_category( $post_id );
    $category_name = $post_category[0]->name;

    if ($include_text !== false) :
        $output = '<div class="catIcon__wrapper">' . check_category_icon($category_name) . '<span class="catIcon__name">' . $category_name . '</span></div>';
    else:
        $output = '<div class="catIcon__wrapper">' . check_category_icon($category_name) . '</div>';
    endif;

    return $output;
}

function check_category_icon($category_name) {
    if($category_name === "Podcast"):
        return '<span class="fas fa-headphones"></span>';
    elseif ($category_name === "Article"):
        return '<span class="fas fa-link"></span>';
    elseif ($category_name === "Blog Post"):
        return '<span class="fas fa-newspaper"></span>';
    elseif ($category_name === "Tv-show"):
        return '<span class="fas fa-desktop"></span>';
    else: 
        return '<span class="fas fa-sticky-note"></span>';
    endif;
}