簡體   English   中英

使用foreach通過Wordpress類別循環

[英]Looping through Wordpress categories with 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()提供的參數無效”

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/

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

解決方案是使用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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM