简体   繁体   中英

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 for a post and 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>';
            }
        }
endif;

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

get_the_category_list() actually generates html list.

Try to use get_categories() instead.

Please refer https://developer.wordpress.org/reference/functions/get_the_category_list/ , https://developer.wordpress.org/reference/functions/get_categories/

you can get the category name by using get_categories() Code is here:

<?php
            $category_args = array(
                'type'                     => 'your_post_type_slug',
                'child_of'                 => 0,
                'parent'                   => 0,
                'orderby'                  => 'id',
                'order'                    => 'ASC',
                'hide_empty'               => 0,
                'hierarchical'             => 1,
                'exclude'                  => '',
                'include'                  => '',
                'number'                   => '',
                'taxonomy'                 => 'your_taxonomy_slug',
                'pad_counts'               => false 

            ); 
            $categories_array = get_categories( $category_args );
             echo '<div class="entry-categories"><span class="sr-only">'. esc_html__( 'Posted in ', 'positor' ) . '</span>';

            foreach ( $categories_array as $categories_val ) {
                echo '<span class="badge badge-primary"><a href="'.get_term_link(intval($categories_val->term_id), $categories_val->taxonomy).'"></span>'.$categories_val->name.'</a>';
            }
        ?>

The solution was to use get_the_category() . Pasted working code below, in case of anybody googling for this later.

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();
        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 mr-1">';
                echo $category->name;
                echo '</span>';

            }
            echo '</div>';
            }
        }
endif;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM