dongtao9158 2013-07-01 01:27
浏览 49

wp_list_pages具有顺序编号的当前页面的子项

I'm currently using this code to output (using wp_list_pages) a list of the current page children.

<?
if(wp_list_pages('child_of='.$post->ID.'&echo=0&exclude=2')) {
wp_list_pages('title_li=&child_of='.$post->ID.'&exclude=2');
}
elseif(get_the_title($post->post_parent) != the_title(' ' , ' ',false)) {
wp_list_pages('child_of='.$post->post_parent.'&title_li=&exclude=2');
} ?>

I would like to merge this code with another piece that enables me to add sequential numbering to the li elements, as shown here.

<?php $list_of_pages = wp_list_pages('echo=0&title_li=&depth=1' );
$list_pages = explode('<li class="',$list_of_pages); $len = count($list_pages);
for ($i = 0; $i <= $len-2; $i++) :
echo $list_pages[$i] . '<li  class="link-' . ($i+1) . ' ';
endfor; echo $list_pages[$i]; ?>

But i'm not particularly handy with PHP, if anyone could point me in the right direction to use the two snippets of code combined it would be much appreciated.

  • 写回答

1条回答 默认 最新

  • dongle3217 2013-07-01 22:15
    关注

    It is a bit messy but it works and there is always room for refactoring the code below in the future. The code below first checks to see if a page is the parent of child page. If it is the child page it will only show the child. It adds an incremental counter to every list item(even the children) and the counter is initiated by calling echo=0. Hope this helps

    <?php
    
        if(wp_list_pages('child_of='.$post->ID.'&echo=0&exclude=2')) {
            $list_of_pages = wp_list_pages('echo=0&title_li=&child_of='.$post->ID.'&exclude=2');
            $list_pages = explode('<li class="',$list_of_pages); $len = count($list_pages);
            for ($i = 0; $i <= $len-2; $i++) :
            echo $list_pages[$i] . '<li class="link-'.($i+1). ' ';
            endfor; echo $list_pages[$i]; 
        }
    
        elseif(get_the_title($post->post_parent) != the_title(' ' , ' ',false)) {
        $list_of_pages = wp_list_pages('echo=0&child_of='.$post->post_parent.'&title_li=&exclude=2');
        $list_pages = explode('<li class="',$list_of_pages); $len = count($list_pages);
        for ($i = 0; $i <= $len-2; $i++) :
        echo $list_pages[$i] . '<li class="link-'.($i+1). ' ';
        endfor; echo $list_pages[$i];
    
    } ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装