0

基本上我想以这种格式按月显示帖子。我已经在这个网站上搜索和搜索了几个小时,但我找不到一个正是我想要的解决方案。任何帮助是极大的赞赏。谢谢!

我在 HTML 中尝试做的事情 -

<!--JUNE-->
<a href="#" class="trigger current">June 2013</a>
<div class="pane wdiv">
  <ul>
    <li>Post Title</li>
    <li>Post Title</li>
  </ul>

   <ul>
      <li>Post Title</li>
      <li>Post Title</li>
   </ul>

   <ul>
      <li>Post Title</li>
      <li>Post Title</li>
   </ul>

<div style="clear:both;"></div>
</div>

<!--May-->
<a href="#" class="trigger current">May 2013</a>
<div class="pane wdiv">
  <ul>
    <li>Post Title</li>
    <li>Post Title</li>
  </ul>

   <ul>
      <li>Post Title</li>
      <li>Post Title</li>
   </ul>

   <ul>
      <li>Post Title</li>
      <li>Post Title</li>
   </ul>

<div style="clear:both;"></div>
</div>

在过去的 3 个小时里,我一直在尝试和玩弄什么,但没有运气 -

<?php global $post;

$args = array( 'numberposts' => 5,);

$myposts = get_posts( $args );

foreach( $myposts as $post ) :  setup_postdata($post); ?>

<?php the_date('F Y','<a href="#" class="trigger current">','</a>'); ?> 

   <div class="pane wdiv">
      <ul>
         <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
      </ul>
      <div style="clear:both;"></div>
   </div>

<?php endforeach; ?>  

正在显示什么

<!--JUNE-->
<a href="#" class="trigger current">June 2013</a> 

   <div class="pane wdiv">
      <ul>
         <li>Test Post</li>
      </ul>
      <div style="clear:both;"></div>
   </div>

   <div class="pane wdiv">
      <ul>
         <li>Test Post2</li>
      </ul>
      <div style="clear:both;"></div>
   </div>

<!--May-->
<a href="#" class="trigger current">May 2013</a> 

   <div class="pane wdiv">
      <ul>
         <li>Awesome Video</li>
      </ul>
      <div style="clear:both;"></div>
   </div>


   <div class="pane wdiv">
      <ul>
         <li>Recommended Readings</li>
      </ul>
      <div style="clear:both;"></div>
   </div>


   <div class="pane wdiv">
      <ul>
         <li>Recommended Readings</li>
      </ul>
      <div style="clear:both;"></div>
   </div>
4

1 回答 1

1

您遇到的问题是您正在循环浏览帖子并且您将获得每个帖子的日期。相反,您想检测您所在的月份。这是我的循环的样子。

<?php
if (!empty($myposts)) :
global $post;
    $month = date( 'n', strtotime($myposts[0]->post_date) );
    ?>
    <a href="#" class="trigger current"><?php echo date('F Y', strtotime($myposts[0]->post_date)) ?>
    <div class="pane wdiv">
    <ul>
    <?php
    foreach ( $myposts as $post ) : 
        setup_postdata($post);
        if (get_the_time('n') !== $month) : ?>
        </ul>
        <div style="clear:both;"></div>
        </div>
        <a href="#" class="trigger current"><?php the_time('F Y') ?></a>
        <div class="pane wdiv">
        <ul>
        <?php endif; ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>
    <div style="clear:both;"></div>
    </div>
<?php endif; ?>

现在它让我根据以下评论进行编辑,回复编辑:

<?php
if (!empty($myposts)) :
    global $post;
    $month = date( 'n', strtotime($myposts[0]->post_date) );
    ?>
    <a href="#" class="trigger current"><?php echo date('F Y', strtotime($myposts[0]->post_date)) ?>
    <div class="pane wdiv">
    <ul>
    <?php
    foreach ( $myposts as $post ) : 
        setup_postdata($post);
        if (get_the_time('n') !== $month) : ?>
        </ul>
        <div style="clear:both;"></div>
        </div>
        <a href="#" class="trigger current"><?php the_time('F Y') ?></a>
        <div class="pane wdiv">
        <ul>
        <?php endif; ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php 
        $month = get_the_time('n');
    endforeach; ?>
    </ul>
    <div style="clear:both;"></div>
    </div>
<?php endif; ?>
于 2013-08-08T19:54:49.437 回答