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 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错