Looping through WordPress categories with foreach

I guess this for sure is a newbie question, but i tried to modify some code found on other questions on this, and I still cannot find out how to loop through categories to make them appear as Bootstrap 4 badges.

if ( ! function_exists( 'mytheme_the_categories' ) ) :
 * Prints HTML with the categories, formatted as Bootstrap 4 badges. 
function mytheme_the_categories() {
        $categories_list = get_the_category_list();
        if ( $categories_list && positor_categorized_blog() ) {
            echo '<div class="entry-categories"><span class="sr-only">'. esc_html__( 'Posted in ', 'positor' ) . '</span>';

            foreach ($categories_list as $category) {
                echo '<span class="badge badge-primary">';
                echo $category->name;
                echo '</span>';

            echo '</div>';

However gets an error: “Warning: Invalid argument supplied for foreach()”

Read more here: Looping through WordPress categories with foreach

Leave a Reply

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