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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路