【问题标题】:Looping through Wordpress categories with foreach使用 foreach 循环遍历 Wordpress 类别
【发布时间】:2017-05-06 10:05:14
【问题描述】:

我想这肯定是一个新手问题,但我尝试修改在其他问题上找到的一些代码,但我仍然无法找到如何循环浏览帖子的类别并使它们显示为 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() 提供的参数无效”

【问题讨论】:

    标签: php wordpress foreach


    【解决方案1】:

    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/

    【讨论】:

      【解决方案2】:

      您可以使用 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>';
                  }
              ?>
      

      【讨论】:

        【解决方案3】:

        解决方案是使用get_the_category()。在下面粘贴工作代码,以防以后有人在谷歌上搜索。

        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;
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-02-24
          • 1970-01-01
          • 2013-03-17
          • 1970-01-01
          • 1970-01-01
          • 2015-02-13
          • 1970-01-01
          • 2013-12-16
          相关资源
          最近更新 更多