Function Reference/Walker Class

来自站长百科
Fludlen讨论 | 贡献2008年8月1日 (五) 10:15的版本
跳转至: 导航、​ 搜索

Role of Walker

Walker角色

The walker class encapsulates the basic functionality necessery to output html representing wordpress objects with a tree structure. For instance pages and categories are the two types of objects that the wordpress 2.2 code uses the walker class to enumuerate. While one could always display categories as you wished by using WordPress:right func and looping through them using it takes a great deal of work to arrange subcategories below their parents with proper formating. The walker class takes care of most of this work for you.

walker class压缩了用来输出html,用树结构代表WordPress的基本函数。例如,网页和类别是两种类型的objects,WordPress2.2代码使用walker class来enumuerate。虽然通过使用right fuc并且looping through them用它来显示多数操作,在母类别下安排子类别,你可以总是显示类别。walker class为你执行大多数这样的操作。

Methods and 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.

注意Walker class的属性通常是有扩展的class设置而且可能在一个例子中不会更改。

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.

同时start_el, end_el, start_lvl, end_lvl的定义方法,只是列出了一个参数,但是使用call_user_func_array调用,正是列出了这些参数。

Properties

属性

$tree_type
The classes extended Walker in the wordpress 2.2 distribution set this either to 'category' or 'page'. The code makes no use of this value.
$tree_type
classes在WordPress2.2版本中扩展了Walker,并且将这个设置为'类别' 或者 '网页'。这个代码没有使用这个参数值。
$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.
$db_fields
拥有keys的数组。parentid。这些keys的参数值应该是objects walker中的属性的名称,应该用来分别放入当前object和母object的id。

Example

例子

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


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

Thus the Walker_Page class (part of wordpress 2.2) 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.

因此Walker_Page class(WordPress 2.2版本的一部分)期望如果page是个网页object那么 page->post_parent会给出那个网页的母网页的id而且page->ID会给出那个网页的id。

Methods

方法

walk($elements, $to_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($elements, $to_depth)
得到一组有序的元素,这样子在的下面而且整数$to_depth。非零的$to_depthcaps方法下传的最大深度。如果$to_depth是 -1,array进行操作时候,就像是平的(没有子元素)。任何传递给walk的额外参数,会传递,为改变到其它的方法walk调用。


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.

walk一个一个地通过数组$elements。每次当元素是先前元素的子元素的时候walk,调用start_lvl。每次元素作为walk调用处理的时候,调用start_el然后调用end_el。每次元素不再在当前母walk的下面的时候,调用end_lvl

start_el($output, $element, $depth, \[optional args\])
Classes extending Walker should define this function to return $output concatenated with the markup starting an element.
start_el($output, $element, $depth, \[optional args\])
Classes 扩展Walker应该定一个这个函数返回$output与开始一个元素的标记相连接。
end_el($output, $element, $depth, \[optional args\])
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.
end_el($output, $element, $depth, \[optional args\])
Classes extending Walker应该定义这个函数返回$output与结束元素的标记相连接。注意只有添加了这些元素的子元素之后,这些元素才会结束。
start_lvl($output, $depth, \[optional args\])
Classes extending Walker should define this function to return $output concatenated with the markup that should precede any of the child elements. For instance this often outputs a ul or ol tag.
start_lvl($output, $depth, \[optional args\])
Classes extending Walker应该定义这个函数返回$output与子元素之前标记相连接。例如,这个通常输出ul或者ol标签。
end_lvl($output, $depth, \[optional args\])
Classes extending Walker should define this function to return $output concatenated with the markup that should end any of the child elements. For instance this often ends a ul or ol tag.
end_lvl($output, $depth, \[optional args\])
Classes extending Walker应该定义这个函数返回$output,与结束任何子元素的标记相连接。例如,这个通常结束ul或者ol标签。

Examples

例子

See WordPress:Function_Reference/Walker_Page, WordPress:Function_Reference/Walker_PageDropDown, WordPress:Function_Reference/Walker_Category, WordPress:Function_Reference/Walker_CategoryDropdown

请看看 Function_Reference/Walker_Page, Function_Reference/Walker_PageDropDown, Function_Reference/Walker_Category, Function_Reference/Walker_CategoryDropdown