I’d like get_the_category_list to only display one or two categories instead of all the categories associated with the post. Haven’t been able to find any results.

<?php echo get_the_category_list(); ?>

Any help would be appreciated

4 Answers
4

$categories = get_the_category();
if ( ! empty( $categories ) ) {
    echo '<a href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a>';
}

Leave a Reply

Your email address will not be published. Required fields are marked *