Class Reference/Walker

来自WordPress中文文档
跳转至: 导航搜索

includeonlydiv style=clear:both; background-color:#FAEBD7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;This page is marked as incomplete. You can help Codex by expanding it./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude includeonlydiv style=clear:both; background-color:#EEEEFF; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;This article is marked as in need of editing. You can help Codex by editing it./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude

Introduction

The Walker class was implemented in WordPress 2.1 to provide developers with a means to traverse tree-like data structures for the purpose of rendering HTML.

Tree-Like Structures

In terms of web development, a tree-like structure is an object with hierarchical data - such that it can be visually represented with a root element and subtrees of children.

Examples of objects with data that can be structured in a "tree-like" way include navigational menus, page categories, and breadcrumbs.

Role of Walker

The Walker class is an abstract class. By itself, the Walker class simply "walks" through each node in a tree. It needs to be extended in order to take an action at one of these nodes. The most common purpose to using this class is to render HTML around tree-like structures.

Usage

Although the Walker class has many uses, one of the most common usages by developers is outputting HTML for highly customized menus (usually ones that have been defined using the WordPress Admin's Menus Screen).

This class is not used by itself, but must be extended. Many of it's methods are abstract* and therefore must be overridden by the developer.

Note: The Walker class was created prior to PHP5 and so does not make use of PHP5's explicit abstraction keywords or features. In this case, the class and it's methods are implicitly abstract (PHP4 compatible) and not explicitly abstract (PHP5 compatible). Developers are not required to implement any methods of the class, and may use or override only those classes that are needed.

Methods & Properties

Note that the properties of the Walker class are intended to be set by the extending class and probably should not vary over the lifetime of an instance.

Also the method definitions of start_el, end_el, start_lvl, end_lvl only list one argument but are called using call_user_func_array and it is these arguments that are listed.

Properties

$tree_type 
The Walker class itself makes no use of this value, although it may be useful to developers. Internally, WordPress's own extended Walker classes will set this to values like 'category' or 'page'.
$db_fields 
An array with keys: parent and id. The value for these keys should be the name of the property in the objects walker will be applied to holding the id of the current object and parent object respectively.
$max_pages 
The maximum number of pages walked by the paged walker.

Abstract Methods

These methods should be defined in the child class, as needed.

start_lvl( &$output, $depth, $args ) 
This method is abstract and must be defined by the developer in a child class. This method is run when the walker reaches a new "branch" in a tree structure. Generally, this method is used to output the opening tag of an HTML element (such as <ol>, <ul>, or <div>).
end_lvl( &$output, $depth, $args ) 
This method is abstract and must be defined by the developer in a child class. This method is run when the walker reaches the end of a "branch" in a tree structure. Generally, this method is used to output the closing tag of an HTML element (such as </ol>, </ul>, or </div>).
start_el( &$output, $element, $depth, $args ) 
This method is abstract. Classes extending Walker should define this function to return $output concatenated with the markup starting an element.
end_el( &$output, $element, $depth, $args ) 
This method is abstract. Classes extending Walker should define this function to return $output concatenated with the markup ending an element. Note that elements are not ended until after all of their children have been added.

Public Methods

These methods are defined by the parent class and may be called from within child methods as needed.

walk($elements, $max_depth) 
Takes an array of elements ordered so that children occur below their parents and an integer $to_depth. A non-zero $to_depth caps the maximum depth to which the method will descend. If $to_depth is -1 the array is processed as if it was flat (no element is the child of another). Any additional arguments passed to walk will be passed unchanged to the other methods walk calls. walk steps through the array $elements one by one. Each time an element is the child of the prior element walk calls start_lvl. Every time an element is processed walk calls start_el and then end_el. Each time an element is no longer below one of the current parents walk calls end_lvl.
paged_walk( $elements, $max_depth, $page_num, $per_page ) 
Given an array of hierarchical elements, the maximum depth, a specific page number, and number of elements per page, this function first determines all top level root elements belonging to that page, then lists them and all of their children in hierarchical order.
get_number_of_root_elements( $elements ) 
Returns the number of elements in a given object.
unset_children( $e, &$children_elements ) 
Unsets all the children for a given top-level element.

Private Methods

display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) 
This method should be considered private and should not be called directly. Use walk() instead.

Examples

Property Usage Example

class Walker_Page extends Walker {
	var $tree_type = 'page';
	var $db_fields = array (
		'parent' => 'post_parent', 
		'id' => 'ID'
	);
	// OTHER CODE...
}

Thus the Walker_Page class expects that if page is a page object then page->post_parent will give the id of that page's parent and page->ID will give the id of that page.

Source File

Walker is located in onlyincludecodewp-includes/class-wp-walker.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn

External Resources

Related

模板:Walker Classes

模板:Class Footer