如何通过 php 自动创建 li 元素


How to create li elements automatically by php?

最近,我通过wordpress建立了一个网站。 我已经使用ul创建了一个侧边栏...李元素。 我手动编写了代码,如下所示。

<ul class="accordion">
    <li id="one"><a href="#one">2010 News</a>
        <?php query_posts('order=ASC&cat=2&tag=2010&orderby=date'); ?>
        <ul class="sub-menu">
            <?php while (have_posts()) : the_post(); ?>  
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
        <?php endwhile;?> 
        </ul>
    </li>
    <li id="two"><a href="#two">2011 News</a>
        <?php query_posts('order=ASC&cat=2&tag=2011&orderby=date'); ?>
        <ul class="sub-menu">
            <?php while (have_posts()) : the_post(); ?>  
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
        <?php endwhile;?> 
       </ul>
    </li>
</ul>

我只希望代码在有人在 id 为 2 的类别中发布新页面并用 2012 或 2013 签名标签时,通过添加 li 元素自动创建 2012 News,*2013 News* 部分。 我可以自己修改代码,但其他人可能不知道如何修改它。 所以我只是想让其他人更容易。 谁能帮我? 多谢。

<ul class="accordion">
   <?php for ($year = 2010; $year < date('Y'); $year++) { ?>
    <li id="one"><a href="#<?php echo $year;?>"><?php echo $year;?> News</a>
        <?php query_posts("order=ASC&cat=2&tag=$year&orderby=date"); ?>
        <ul class="sub-menu">
            <?php while (have_posts()) : the_post(); ?>  
                <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>  
            <?php endwhile; ?> 
        </ul>
    </li>
    <?php } ?>
</ul>