2017-02-22 7 views
0

私のコードはほとんど動作していますが、小さな問題があります。Wordpressのすべてのカテゴリと投稿のリスト

私はこの出力を見ることを期待しています...

  • 国内
  • Ragdol

  • パグ
  • ブラッドハウンド

しかし、その代わりに、私は取得しています:

  • 国内
  • Ragdol
  • パグ
  • ブラッドハウンド

  • 国内
  • Ragdol
  • パグ
  • ブラッドハウンド

あなたの代わりに、すべてのポストは、各カテゴリにリストされている見ることができるようにそれぞれのためのちょうどポストの(申し訳ありません - 意図しない馬小屋)それぞれのカテゴリ。

ここにありますが、私のコードです:

<?php 
$cat_args = array(
'taxonomy' => 'animal_category' 
); 

$categories = get_categories($cat_args); 

foreach ($categories as $category) { 
$category_hero = get_field('hero', $taxonomy . '_' . $category->term_id); ?> 

<div class="gallery"> 

    <div class="gallery-hero"> 
     <h2><?php echo $category->name; ?></h2> 
     <img src="<?php echo $category_hero["sizes"]["Full"]; ?>" /> 
    </div> 


    <?php 
    $cat_ID = $category->id; 
    $post_args = array(
      'showposts'   => -1, 
      'post_type'   => 'gallery', 
      'offset'   => 0, 
      'category'   => $cat_ID 
     ); 
    $posts = get_posts($post_args); 

    foreach($posts as $post) { ?> 
     <div class="gallery-box"> 
      <?php $gallery_image = get_field("photos"); ?> 
      <a href="<?php the_permalink() ?>"> 
       <img src="<?php echo $gallery_image[0]["sizes"]["Medium"]; ?>" /> 
       <span><?php the_title(); ?></span> 
      </a> 
     </div> 
    <?php } ?> 


</div> 

すべてが私には右に見えます。私は間違って何をしていますか?

+0

このリンクをチェック:http://stackoverflow.com/questions/25502227/display-posts-category-wise –

答えて

1

コードに誤りがあります。次のコードで使用してください。私はこれが正しく動作することを願っています。

<?php 
$cat_args = array(
    'taxonomy' => 'animal_category' 
); 

$categories = get_categories($cat_args); 
foreach ($categories as $category) { 
$category_hero = get_field('hero', $taxonomy . '_' . $category->term_id); ?> 
<div class="gallery"> 

    <div class="gallery-hero"> 
     <h2><?php echo $category->name; ?></h2> 
     <img src="<?php echo $category_hero["sizes"]["Full"]; ?>" /> 
    </div> 


    <?php 
    $cat_ID = $category->term_id; 
    $post_args = array(
      'showposts'   => -1, 
      'post_type'   => 'gallery', 
      'offset'   => 0, 
      //'category'   => $cat_ID 
      'tax_query' => array(
       array(
        'taxonomy' => 'animal_category', 
        'field' => 'id', 
        'terms' => $cat_ID 
       ) 
      ) 
     ); 
    $posts = get_posts($post_args); 

    foreach($posts as $post) { ?> 
     <div class="gallery-box"> 
      <?php $gallery_image = get_field("photos"); ?> 
      <a href="<?php the_permalink() ?>"> 
       <img src="<?php echo $gallery_image[0]["sizes"]["Medium"]; ?>" /> 
       <span><?php the_title(); ?></span> 
      </a> 
     </div> 
    <?php } ?> 
</div> 
<?php } ?> 
関連する問題