Function Reference/Walker Class:修订间差异

来自站长百科
跳转至: 导航、​ 搜索
无编辑摘要
无编辑摘要
 
第1行: 第1行:
== Role of Walker ==
== 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。虽然通过使用[[WordPress:right func|right fuc]]并且looping through them用它来显示多数操作,在母类别下安排子类别,你可以总是显示类别。walker class为你执行大多数这样的操作。
walker class压缩了用来输出html,用树结构代表WordPress的基本函数。例如,网页和类别是两种类型的objects,WordPress2.2代码使用walker class来enumuerate。虽然通过使用[[WordPress:right func|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设置而且可能在一个例子中不会更改。
注意Walker class的属性通常是有扩展的class设置而且可能在一个例子中不会更改。
 
   
Also the method definitions of <tt>start_el, end_el, start_lvl, end_lvl</tt> only list one argument but are called using call_user_func_array and it is these arguments that are listed.  
 
同时<tt>start_el, end_el, start_lvl, end_lvl</tt>的定义方法,只是列出了一个参数,但是使用call_user_func_array调用,正是列出了这些参数。
同时<tt>start_el, end_el, start_lvl, end_lvl</tt>的定义方法,只是列出了一个参数,但是使用call_user_func_array调用,正是列出了这些参数。
===Properties===


===属性===
===属性===
; <tt>$tree_type</tt> : 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. 
; <tt>$tree_type</tt> : classes在WordPress2.2版本中扩展了Walker,并且将这个设置为'类别' 或者 '网页'。这个代码没有使用这个参数值。
; <tt>$tree_type</tt> : classes在WordPress2.2版本中扩展了Walker,并且将这个设置为'类别' 或者 '网页'。这个代码没有使用这个参数值。
; <tt>$db_fields</tt> : An array with keys: <tt>parent</tt> and <tt>id</tt>.  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.


; <tt>$db_fields</tt> :拥有keys的数组。<tt>parent</tt> 和<tt>id</tt>。这些keys的参数值应该是objects walker中的属性的名称,应该用来分别放入当前object和母object的id。
; <tt>$db_fields</tt> :拥有keys的数组。<tt>parent</tt> 和<tt>id</tt>。这些keys的参数值应该是objects walker中的属性的名称,应该用来分别放入当前object和母object的id。


====Example====
====例子====
====例子====
<code><pre>
<code><pre>
第40行: 第22行:
</pre>
</pre>
</code>
</code>
<code><pre>
class Walker_Page extends Walker {
var $tree_type = 'page';
var $db_fields = array ('parent' =>
                  'post_parent', 'id' => 'ID');
</pre>
</code>
Thus the Walker_Page class (part of wordpress 2.2) expects that if <tt>page</tt> is a page object then <tt>page->post_parent</tt> will give the id of that page's parent and <tt>page->ID</tt> will give the id of that page.


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


===Methods===
===方法===
===方法===
 
; <tt>walk($elements, $to_depth)</tt> :得到一组有序的元素,以便子元素在母元素和整数<tt>$to_depth</tt>下面。非零的<tt>$to_depth</tt>指出了可以嵌套的的最大的层级。如果<tt>$to_depth</tt>是 -1,array进行操作时候,就像是平的(没有子元素)。任何传递给<tt>walk</tt>的额外参数,会传递,为改变到其它的类别<tt>walk</tt>调用。
; <tt>walk($elements, $to_depth)</tt> : Takes an array of elements ordered so that children occur below their parents and an integer <tt>$to_depth</tt>.  A non-zero <tt>$to_depth</tt> caps the maximum depth to which the method will descend.  If <tt>$to_depth</tt> is -1 the array is processed as if it was flat (no element is the child of another).  Any additional arguments passed to <tt>walk</tt> will be passed unchanged to the other methods <tt>walk</tt> calls.
<tt>walk</tt>一个一个地通过数组<tt>$elements</tt>。每次当元素是先前元素的子元素的时候<tt>walk</tt>,调用<tt>start_lvl</tt>。每次元素作为<tt>walk</tt>调用处理的时候,调用<tt>start_el</tt>然后调用<tt>end_el</tt>。每次元素不再当前母<tt>walk</tt>的下面的时候,调用<tt>end_lvl</tt>。
 
; <tt>walk($elements, $to_depth)</tt> :得到一组有序的元素,这样子在的下面而且整数<tt>$to_depth</tt>。非零的<tt>$to_depth</tt>caps方法下传的最大深度。如果<tt>$to_depth</tt>是 -1,array进行操作时候,就像是平的(没有子元素)。任何传递给<tt>walk</tt>的额外参数,会传递,为改变到其它的方法<tt>walk</tt>调用。
 
 
<tt>walk</tt> steps through the array <tt>$elements</tt> one by one.  Each time an element is the child of the prior element <tt>walk</tt> calls <tt>start_lvl</tt>.  Every time an element is processed <tt>walk</tt> calls <tt>start_el</tt> and then <tt>end_el</tt>.  Each time an element is no longer below one of the current parents <tt>walk</tt> calls <tt>end_lvl</tt>. 
 
<tt>walk</tt>一个一个地通过数组<tt>$elements</tt>。每次当元素是先前元素的子元素的时候<tt>walk</tt>,调用<tt>start_lvl</tt>。每次元素作为<tt>walk</tt>调用处理的时候,调用<tt>start_el</tt>然后调用<tt>end_el</tt>。每次元素不再在当前母<tt>walk</tt>的下面的时候,调用<tt>end_lvl</tt>。
      
      
; <tt>start_el($output, $element, $depth, \[optional args\])</tt> : Classes extending <tt>Walker</tt> should define this function to return <tt>$output</tt> concatenated with the markup starting an element.   
; <tt>start_el($output, $element, $depth, \[optional args\])</tt> : Classes extending <tt>Walker</tt> should define this function to return <tt>$output</tt> concatenated with the markup starting an element.   

2008年8月2日 (六) 13:02的最新版本

Walker角色[ ]

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

方法和属性[ ]

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

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

属性[ ]

$tree_type
classes在WordPress2.2版本中扩展了Walker,并且将这个设置为'类别' 或者 '网页'。这个代码没有使用这个参数值。
$db_fields
拥有keys的数组。parentid。这些keys的参数值应该是objects walker中的属性的名称,应该用来分别放入当前object和母object的id。

例子[ ]

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

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

方法[ ]

walk($elements, $to_depth)
得到一组有序的元素,以便子元素在母元素和整数$to_depth下面。非零的$to_depth指出了可以嵌套的的最大的层级。如果$to_depth是 -1,array进行操作时候,就像是平的(没有子元素)。任何传递给walk的额外参数,会传递,为改变到其它的类别walk调用。

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