dongtu9823 2016-03-02 16:15 采纳率: 0%
浏览 52
已采纳

Wordpress时间线显示帖子每月

I am fetching data through custom post type and category. I have added 12 category Jan to Dec and Jan has 1 posts Feb have 2 post

What I am struggling to do on January post the 2 circle is showing on the left I want only one January Circle rest of the January post under the circle.

Can you please how can i put check on category?

here is the site http://novartis.portlandvault.com/timeline/

Thanks

<div id="timeline">
    <?php
        //Define your custom post type name in the arguments

        $args = array('post_type' => 'timeline', 'order' => 'asc' );

        //Define the loop based on arguments

        $loop = new WP_Query( $args );

        //Display the contents

        while ( $loop->have_posts() ) : $loop->the_post();
        $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'large') );
        $category = get_the_terms($id, 'timeline_categories'); 
        ?>

    <div class="timeline-item">
        <div class="timeline-icon">
            <div class="timeline-month">
                <?php echo $category[0]->name;?>
            </div>
        </div>
        <div class="timeline-content right">
            <h2>
                <?php the_title(); ?>
            </h2>
            <p>
                <?php echo the_content(); ?>
            </p>
            <div class="timeline_img">
                <img src="<?php echo $thumb; ?>" class="img-responsive">
            </div>
        </div>

    </div>

    <?php endwhile;?>
</div>
<!-- Timeline ends here -->
  • 写回答

1条回答 默认 最新

  • dongyo1818 2016-03-02 18:43
    关注

    I am not sure using categories is the best way to do this. You are limiting yourself to one years worth of data. I would suggest actually using the post date to separate the posts, then your code could look something like this.

    <div id="timeline">
      <?php
        //Define your custom post type name in the arguments
        $args = array('post_type' => 'timeline', 'order' => 'asc' );
    
        //Define the loop based on arguments
        $loop = new WP_Query( $args );
    
        //Display the contents
        while ( $loop->have_posts() ) : $loop->the_post();
        $thumb = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'large') );
        $category = get_the_terms($post->ID, 'timeline_categories'); 
        ?>
        <section id="<?php echo $post->ID; ?>">
    
          <?php
            if( $loop->current_post === 0 ) {
              $current_quarter = $category[0]->name; ?>
              <div class="quarterlyheading">
                <?php echo $current_quarter; ?>
                <div class="quarterlinebreak"><hr></div>
              </div>
            <?php } else {      
              $post_quarter = $category[0]->name;
              if($current_quarter != $post_quarter) { ?>
                <div class="quarterlyheading">
                  <?php echo $post_quarter; ?>
                  <div class="quarterlinebreak"><hr></div>
                </div>
              <?php }
            }
            $current_quarter = $post_quarter;
          ?>
    
          <div class="timeline-item">
            <?php
              if( $loop->current_post === 0 ) {
                $current_month = get_the_time('M'); ?>
                <div class="timeline-icon">
                  <div class="timeline-month">
                    <?php echo $current_month; ?>
                  </div>
                </div>
              <?php } else {      
                $post_month = get_the_time('M');
                if($current_month != $post_month) { ?>
                  <div class="timeline-icon">
                    <div class="timeline-month">
                      <?php echo $post_month; ?>
                    </div>
                  </div>
                <?php }
              }
              $current_month = $post_month;
            ?>        
            <div class="timeline-content right">
    
              <h2>
                <?php the_title(); ?>
              </h2>
              <p>
                <?php echo the_content(); ?>
              </p>
              <div class="timeline_img">
                <img src="<?php echo $thumb; ?>" class="img-responsive">
              </div>
            </div>
    
          </div>
        </section>
      <?php endwhile;?>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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