@charset "UTF-8";
/*
    Template: swell
    Theme Name: SWELL CHILD
    Theme URI: https://swell-theme.com/
    Description: SWELLの子テーマ
    Version: 1.0.0
    Author: LOOS WEB STUDIO
    Author URI: https://loos-web-studio.com/

    License: GNU General Public License
    License URI: http://www.gnu.org/licenses/gpl.html
*/

/* works CPT のカテゴリー表示をカスタムタクソノミー（work_type）に差し替え */
add_filter( 'swell_post_list_cat_data', function( $cat_data, $post_id ) {
    if ( get_post_type( $post_id ) !== 'works' ) return $cat_data;

    $terms = get_the_terms( $post_id, 'work_type' );
    if ( empty( $terms ) || is_wp_error( $terms ) ) return $cat_data;

    return [
        'id'   => $terms[0]->term_id,
        'name' => $terms[0]->name,
    ];
}, 10, 2 );

/* works CPT のカテゴリー表示をカスタムタクソノミーに差し替え */
function swl_parts__post_list_category( $args ) {
    $the_id = $args['post_id'] ?? get_the_ID();
    $class  = $args['class']   ?? 'p-postList__cat u-thin';

    if ( get_post_type( $the_id ) === 'works' ) {
        // worksはカスタムタクソノミーを使用
        $terms = get_the_terms( $the_id, 'work_type' );
        if ( empty( $terms ) || is_wp_error( $terms ) ) return;
        echo '<span class="' . esc_attr( $class ) . ' icon-folder" data-cat-id="' . esc_attr( $terms[0]->slug ) . '">'
            . esc_html( $terms[0]->name )
            . '</span>';
        return;
    }

    // 通常投稿は標準カテゴリーをそのまま表示
    $cat_data = get_the_category( $the_id );
    if ( empty( $cat_data ) ) return;

    $display_cat = $cat_data[0];
    echo '<span class="' . esc_attr( $class ) . ' icon-folder" data-cat-id="' . esc_attr( $display_cat->slug ) . '">'
        . esc_html( $display_cat->name )
        . '</span>';
}