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条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能