2013-04-23 57 views
0

我有以下的頁面結構:WordPress的 - 隱藏子頁面的兒童在wp_list頁

  • 主頁(ID 5)
    • 子頁面1
      • 兒童頁
      • 兒童頁
    • 子頁面2
      • 兒童頁
      • 兒童頁

我用我的顯示工具欄菜單下面的代碼:只

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

此代碼顯示一個列表。我需要顯示

  • 當主網頁上,只顯示子頁面
  • 當在子頁面,顯示子頁面和子頁面目前我在
  • 的子頁面的時候,孩子頁面上,顯示我目前所在的父子頁面的顯示子頁面和子頁面

任何想法如何做到這一點? 感謝您的幫助

回答

0

兄弟姐妹我找到了一個工作代碼,就像一個魅力對我來說:

/** 
* Create HTML list of pages. 
* 
* @package Razorback 
* @subpackage Walker 
* @author Michael Fields <[email protected]> 
* @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; 
    } 
} 
1

使用深度處理,你想讓它顯示什麼

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

參考:http://codex.wordpress.org/Function_Reference/wp_list_pages

默認值爲0(顯示所有頁面,包括所有子頁) 。

0 (default) Displays pages at any depth and arranges them hierarchically in nested lists 
    -1 Displays pages at any depth and arranges them in a single, flat list 
    1 Displays top-level Pages only 
    2, 3 … Displays Pages to the given depth 
+0

謝謝,但深度不容許我有選擇地顯示在子頁面,它顯示它們全部,對嗎?我的意思是,當我在子頁面上顯示子頁面並且只顯示子頁面的子頁面時,我將無法執行' – user1049961 2013-04-24 05:51:52

0

你可以嘗試使用此Hierarchical Pages Widget。我不確定它是否支持你想要的,但是我在我的一個項目中使用了它,它運行得很好。

這個小工具將:

製作倒塌分層頁/分類/分類列表:頂級 水平;祖先,孩子,和/或電流

相關問題