首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >列出所有类别和帖子

列出所有类别和帖子
EN

Stack Overflow用户
提问于 2017-02-22 05:57:50
回答 1查看 211关注 0票数 0

我的代码大部分都在工作,但有一个小问题。

我希望看到这个输出..。

  • 国内
  • 拉格多尔

  • 帕格
  • 血犬

但我得到的却是:

  • 国内
  • 拉格多尔
  • 帕格
  • 血犬

  • 国内
  • 拉格多尔
  • 帕格
  • 血犬

正如你所看到的,每一篇文章都被列在每个类别中,而不仅仅是每个类别的帖子(抱歉-无意中的双关语)。

这是我的密码:

代码语言:javascript
复制
<?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>

在我看来一切都是对的。我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-22 06:38:41

您的代码有错误。与以下代码一起使用。我希望这能正常工作。

代码语言:javascript
复制
<?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 } ?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42383761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档