使用foreach通过Wordpress类别循环

时间:2017-05-06 10:05:15

标签: php wordpress foreach

我想这肯定是一个新手问题,但我试图修改其他问题上找到的一些代码,我仍然无法找到如何循环通过类别的帖子,并使它们显示为Bootstrap 4徽章。

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;

但是会出现错误:“警告:为foreach()提供的参数无效”

3 个答案:

答案 0 :(得分:0)

get_the_category_list()实际上生成了html列表。

尝试使用get_categories()代替。

请参阅https://developer.wordpress.org/reference/functions/get_the_category_list/https://developer.wordpress.org/reference/functions/get_categories/

答案 1 :(得分:0)

您可以使用get_categories()获取类别名称 代码在这里:

<?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>';
            }
        ?>

答案 2 :(得分:0)

解决方案是使用get_the_category()。粘贴下面的工作代码,以防任何人稍后使用Google搜索。

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;