dsyua2828 2013-04-23 22:32
浏览 18
已采纳

Wordpress - 在wp_list页面中隐藏子页面的子项

I have following page structure:

  • Main page (id 5)
    • Subpage 1
      • children page
      • children page
    • Subpage 2
      • children page
      • children page

I use this code for displaying my sidebar menu:

 <?php wp_list_pages('child_of=5&title_li=&link_before=<span>&link_after=</span>'); ?>

This code displays only a list. I would need to display

  • when on the main page, display only subpages
  • when on the subpage, display subpages and children pages of the subpage I'm currently on
  • when on the children page, display the display subpages and children pages of the parent subpage I'm currently on

Any idea how to do this? Thanks for help

  • 写回答

3条回答 默认 最新

  • dongqiang8474 2013-04-24 21:31
    关注

    I found a working code, works like a charm for me:

    /**
     * Create HTML list of pages.
     *
     * @package Razorback
     * @subpackage Walker
     * @author Michael Fields <michael@mfields.org>
     * @copyright Copyright (c) 2010, Michael Fields
     * @license http://opensource.org/licenses/gpl-license.php GNU Public License
     *
     * @uses Walker_Page
     *
     * @since 2010-05-28
     * @alter 2010-10-09
     */
    class Razorback_Walker_Page_Selective_Children extends Walker_Page {
        /**
         * Walk the Page Tree.
         *
         * @global stdClass WordPress post object.
         * @uses Walker_Page::$db_fields
         * @uses Walker_Page::display_element()
         *
         * @since 2010-05-28
         * @alter 2010-10-09
         */
        function walk( $elements, $max_depth ) {
            global $post;
            $args = array_slice( func_get_args(), 2 );
            $output = '';
    
            /* invalid parameter */
            if ( $max_depth < -1 ) {
                return $output;
            }
    
            /* Nothing to walk */
            if ( empty( $elements ) ) {
                return $output;
            }
    
            /* Set up variables. */
            $top_level_elements = array();
            $children_elements  = array();
            $parent_field = $this->db_fields['parent'];
            $child_of = ( isset( $args[0]['child_of'] ) ) ? (int) $args[0]['child_of'] : 0;
    
            /* Loop elements */
            foreach ( (array) $elements as $e ) {
                $parent_id = $e->$parent_field;
                if ( isset( $parent_id ) ) {
                    /* Top level pages. */
                    if( $child_of === $parent_id ) {
                        $top_level_elements[] = $e;
                    }
                    /* Only display children of the current hierarchy. */
                    else if (
                        ( isset( $post->ID ) && $parent_id == $post->ID ) ||
                        ( isset( $post->post_parent ) && $parent_id == $post->post_parent ) ||
                        ( isset( $post->ancestors ) && in_array( $parent_id, (array) $post->ancestors ) )
                    ) {
                        $children_elements[ $e->$parent_field ][] = $e;
                    }
                }
            }
    
            /* Define output. */
            foreach ( $top_level_elements as $e ) {
                $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
            }
            return $output;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥15 绘制多分类任务的roc曲线时只画出了一类的roc,其它的auc显示为nan
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?