【问题标题】:Wordpress Timeline Show post MonthlyWordPress 时间线显示帖子每月
【发布时间】:2016-03-02 16:15:57
【问题描述】:

我正在通过自定义帖子类型和类别获取数据。我添加了 12 个类别一月到十二月,一月有 1 个帖子,二月有 2 个帖子

我在 1 月帖子上努力做的事情 2 圆圈显示在左侧

请问如何检查类别?

这里是网站http://novartis.portlandvault.com/timeline/

谢谢

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

【问题讨论】:

  • 感谢安德烈斯的回复。如果我在三月发布一月的帖子怎么办?有没有办法存储类别,而不是检查帖子是否与不显示时间线图标相同的类别,否则如果类别是新的而不是显示时间线图标?这有意义吗?

标签: php css wordpress


【解决方案1】:

我不确定使用类别是否是最好的方法。您将自己限制在一年的数据范围内。我建议实际上使用发布日期来分隔帖子,然后您的代码可能看起来像这样。

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

【讨论】:

  • 嗨,我可以在这个模板上添加季度月吗?比如在与 janaury 到 3 月相关的帖子下首先标题 1 月 - 3 月,然后是 4 月 - 6 月等等..?
  • 我不确定我是否理解您的问题。你的意思是如果你今天发布了一些东西,但想要在一月份之前发布?您需要做的就是更改“发布”日期,使其认为帖子是在一月份发布的。然后它将它分组在一月下并且不添加第二个标签。同样,此方法不使用类别,它使用发布日期。
  • 您好安德烈斯,感谢您的回复。我想要像 novartis.portlandvault.com/screenshot.jpg 这样的东西在顶部显示季度月份,然后显示本月内的帖子
  • 嘿 Atiff,我添加了您的“timeline_categories”,我将使用它来设置 1 月 - 3 月的标题。所以现在当你在你想要的组中标记帖子时,它使用非常相似的逻辑来显示/隐藏“季度标题”部分。希望这是有道理的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 2015-04-02
  • 1970-01-01
  • 2012-10-02
相关资源
最近更新 更多