2017-05-06 4 views
1

これは確かに初心者の質問ですが、私はこれに関する他の質問にあるいくつかのコードを修正しようとしましたが、投稿のカテゴリをループする方法を見つけることができませんブートストラップ4バッジとして表示されます。 "警告:無効な引数がforeachのために供給される()"foreachを使ってWordpressカテゴリをルーピングする

答えて

0

ソリューションは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; 
0

get_the_category_list()実際のhtmlリストを生成

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; 

ただし、エラーを取得します。

代わりにget_categories()を使用してください。参照してくださいhttps://developer.wordpress.org/reference/functions/get_the_category_list/

https://developer.wordpress.org/reference/functions/get_categories/

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>'; 
      } 
     ?> 
関連する問題