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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化