Wordpress选择性页面层次结构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Wordpress选择性页面层次结构相关的知识,希望对你有一定的参考价值。

Originally from:
http://wordpress.mfields.org/2010/selective-page-hierarchy-for-wp_list_pages/
  1. funtions.php:
  2. /**
  3. * Create html list of pages.
  4. *
  5. * @package Razorback
  6. * @subpackage Walker
  7. * @author Michael Fields <[email protected]>
  8. * @copyright Copyright (c) 2010, Michael Fields
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. *
  11. * @uses Walker_Page
  12. *
  13. * @since 2010-05-28
  14. * @alter 2010-10-09
  15. */
  16. class Razorback_Walker_Page_Selective_Children extends Walker_Page {
  17. /**
  18. * Walk the Page Tree.
  19. *
  20. * @global stdClass WordPress post object.
  21. * @uses Walker_Page::$db_fields
  22. * @uses Walker_Page::display_element()
  23. *
  24. * @since 2010-05-28
  25. * @alter 2010-10-09
  26. */
  27. function walk( $elements, $max_depth ) {
  28. global $post;
  29. $args = array_slice( func_get_args(), 2 );
  30. $output = '';
  31.  
  32. /* invalid parameter */
  33. if ( $max_depth < -1 ) {
  34. return $output;
  35. }
  36.  
  37. /* Nothing to walk */
  38. if ( empty( $elements ) ) {
  39. return $output;
  40. }
  41.  
  42. /* Set up variables. */
  43. $top_level_elements = array();
  44. $children_elements = array();
  45. $parent_field = $this->db_fields['parent'];
  46. $child_of = ( isset( $args[0]['child_of'] ) ) ? (int) $args[0]['child_of'] : 0;
  47.  
  48. /* Loop elements */
  49. foreach ( (array) $elements as $e ) {
  50. $parent_id = $e->$parent_field;
  51. if ( isset( $parent_id ) ) {
  52. /* Top level pages. */
  53. if( $child_of === $parent_id ) {
  54. $top_level_elements[] = $e;
  55. }
  56. /* Only display children of the current hierarchy. */
  57. else if (
  58. ( isset( $post->ID ) && $parent_id == $post->ID ) ||
  59. ( isset( $post->post_parent ) && $parent_id == $post->post_parent ) ||
  60. ( isset( $post->ancestors ) && in_array( $parent_id, (array) $post->ancestors ) )
  61. ) {
  62. $children_elements[ $e->$parent_field ][] = $e;
  63. }
  64. }
  65. }
  66.  
  67. /* Define output. */
  68. foreach ( $top_level_elements as $e ) {
  69. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
  70. }
  71. return $output;
  72. }
  73. }
  74.  
  75.  
  76. Insert into template:
  77. /**
  78. * This will walk the navigation tree but exclude the top level pages.
  79. * handy for left-hand-nav links
  80. */
  81. <?php
  82. $root_page_id = ( empty( $post->ancestors ) ) ? $post->ID : end( $post->ancestors );
  83. $walker = new Razorback_Walker_Page_Selective_Children();
  84. wp_list_pages( array(
  85. 'title_li' => '',
  86. 'sort_column' => 'menu_order',
  87. 'child_of' => $root_page_id,
  88. 'walker' => $walker,
  89. ) );
  90. ?>

以上是关于Wordpress选择性页面层次结构的主要内容,如果未能解决你的问题,请参考以下文章

WordPress中具有自定义帖子类型的单个页面的层次结构

Wordpress没有使用home.php而是使用index.php作为帖子页面。

Wordpress - 将代码片段包含到布局的选定部分的插件

Wordpress 不使用 home.php 而是使用 index.php 作为帖子页面。

累积和 - 选择层次结构的部分

分类层次结构不起作用 [Wordpress]