Wordpress 列出所有类别和帖子
Wordpress list all categories and posts
我的代码大部分都能正常工作,但有一个小问题。
我期待看到这个输出...
猫
- 国内
- 布娃娃
狗
- 哈巴狗
- 猎犬
但我得到的是:
猫
- 国内
- 布娃娃
- 哈巴狗
- 猎犬
狗
- 国内
- 布娃娃
- 哈巴狗
- 猎犬
如您所见,每个 post 都列在每个类别中,而不仅仅是每个(抱歉 - 无意的双关语)相应类别的 post。
这是我的代码:
<?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>
我觉得一切都很好。我做错了什么?
您的代码有误。与以下代码一起使用。我希望这会正常工作。
<?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 } ?>
我的代码大部分都能正常工作,但有一个小问题。
我期待看到这个输出...
猫
- 国内
- 布娃娃
狗
- 哈巴狗
- 猎犬
但我得到的是:
猫
- 国内
- 布娃娃
- 哈巴狗
- 猎犬
狗
- 国内
- 布娃娃
- 哈巴狗
- 猎犬
如您所见,每个 post 都列在每个类别中,而不仅仅是每个(抱歉 - 无意的双关语)相应类别的 post。
这是我的代码:
<?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>
我觉得一切都很好。我做错了什么?
您的代码有误。与以下代码一起使用。我希望这会正常工作。
<?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 } ?>