dqndc26628 2013-10-06 02:59
浏览 42
已采纳

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

Recently, I have built a website by wordpress. I have created a sidebar using ul...li elements. I wrote the code manually like the following.

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

I just want the code to create the 2012 News,*2013 News* section automatically by adding the li element when someone has posted a new page in the category whose id is 2 and sign the tag with 2012 or 2013. I can modify the code by myself, but others may not know how to modify it. So I just want to make it easier for others. Can anyone help me? Thanks a lot.

  • 写回答

1条回答 默认 最新

  • douchenchepan6465 2013-10-06 03:08
    关注
    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题