1

我想在我的 wordpress 主题的存档导航中复制以下标记。

HTML:

<ul>
  <li>2012
    <ul>
      <li>December
        <ul>
          <li>Page 1</li>
          <li>Page 2</li>
          <li>Page 3</li>
        </ul>
      </li>
      <li>November
        <ul>
          <li>Page 1</li>
          <li>Page 2</li>
          <li>Page 3</li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

到目前为止,我在另一篇文章中有以下 php,我如何更新此代码以输出像我的 html 年/月/文章标题一样的输出?

    <?php

// Declare some helper vars
$previous_year = $year = 0;
$previous_month = $month = 0;
$ul_open = false;

// Get the posts
$myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC&cat=13');

?>

<?php foreach($myposts as $post) : ?>   

    <?php

    // Setup the post variables
    setup_postdata($post);

    $year = mysql2date('Y', $post->post_date);
    $month = mysql2date('n', $post->post_date);

    ?>

    <?php if($year != $previous_year || $month != $previous_month) : ?>

        <?php if($ul_open == true) : ?>
        </ul>
        <?php endif; ?>

        <h3><?php the_time('F Y'); ?></h3>

        <ul class="month_archive">

        <?php $ul_open = true; ?>

    <?php endif; ?>

    <?php $previous_year = $year; $previous_month = $month; ?>

    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>

<?php endforeach; ?>
    </ul>
4

1 回答 1

0

你试过内置的wp_get_archives()吗?

<?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>

有关此方法的更多信息,请参阅Codex 。

更新

尝试此 WP 论坛帖子末尾建议的插件之一:http ://wordpress.org/support/topic/list-posts-by-year-and-month-archive-like

它们现在已经很老了,我不确定它们是否仍然有效。

于 2013-01-28T15:37:20.187 回答