dongliaojing0554 2018-01-07 13:26
浏览 35
已采纳

仅在wordpress循环中显示类别名称一次

Is there a way to keep my "category name" in the loop, but display it only once for the first post? And not all the posts. I know i can keep it out of the loop, but just wondering if this is possible.

            <?php
                $args = array(
                    'post_type' => 'post',
                    'orderby' => 'date',
                    'order'   => 'ASC',
                    'cat' => 19

                    );

            $catg = new WP_Query($args);

            ?>

            <?php if($catg->have_posts()) : ?>

            <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 lock">

            <?php while($catg->have_posts()) : $catg->the_post() ?>
            <div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 lock">

              <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12"> 
                    <?php
                    $category = get_the_category();
                    ?>
                    <h2 style="text-transform: uppercase;"><?php echo $category[1]->cat_name; ?></h2>                 
            </div>



                <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12 lock">
                    <a href="<?php echo get_permalink( $post->ID ); ?>" class="topico1-link-c">
                        <div class="topico3-col2-c cursos-margem-direita col-xs-12 col-sm-12 col-md-12 col-lg-12" style="padding-left: 0px;">
                            <div class="topico1-img col-xs-12 col-sm-12 col-md-12 col-lg-12 lock">
                                <img src="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>">
                                <div class="imagem-pelicula"></div>
                            </div>

                            <div class="topico1-title col-xs-12 col-sm-12 col-md-12 col-lg-12">
                                <h3 class="h3-low-c"><?php echo get_the_title(); ?></h3>
                            </div>
                        </div>
                    </a>                          
                </div>

            </div>

            <?php endwhile ?>

            <?php endif ?>

            </div>

Also is there a way to get the child category name? When i try this:

                    <?php
                    $category = get_the_category();
                    ?>
                    <h2 style="text-transform: uppercase;"><?php echo 
                    $category[1]->cat_name; ?></h2>    

I get the right information, but if for some reason the parent name comes alphabetically in second, i get the parent name and not the child.

Thank you in advance.

Kind regards!

  • 写回答

2条回答 默认 最新

  • dongliqin6939 2018-01-07 13:38
    关注

    You can use an index that increases automatically with the loop, and an if condition only for the first iteration:

    Before the loop:

    <?php $i = 1; ?>
    

    Inside the loop:

    <?php
      $category = get_the_category();
    ?>
    <h2 style="text-transform: uppercase;">
    <?php
      if($i == 1) { 
        echo $category[1]->cat_name; 
      }
      $i++;
    ?>
    </h2>      
    

    $i == 1 will only be right the first time (then the index is increase by $i++), so in all following loop cycles the category name output will be omitted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分