按月列出具有特定HTML标记的帖子 - Wordpress

时间:2013-08-08 19:42:12

标签: php html wordpress

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

我想用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>

1 个答案:

答案 0 :(得分: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; ?>