doupingzhi9674 2017-02-22 05:57
浏览 58
已采纳

Wordpress列出所有类别和帖子

My code is mostly working but there's a small problem.

I am expecting to see this output...

Cats

  • Domestic
  • Ragdol

Dogs

  • Pug
  • Bloodhound

But instead I am getting:

Cats

  • Domestic
  • Ragdol
  • Pug
  • Bloodhound

Dogs

  • Domestic
  • Ragdol
  • Pug
  • Bloodhound

As you can see every post is being listed in each category, instead of just the posts for each (sorry - unintentional pun) respective category.

Here's is my code:

<?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>

Everything looks right to me. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongmin4990 2017-02-22 06:38
    关注

    You have an error on your code. Use with the following code. I Hope this will work correctly.

    <?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 } ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器