在 WordPress 里 http://localhost/wordpress3.6.1/wp-admin/edit-tags.php?taxonomy=category 这个链接可以显示 WP 里的无限栏目分类,我们来研究一下 WordPress 是如何实现的。
找到 wp-admin/edit-tags.php 这个文件,发现显示栏目的代码很少:
1
2
3
4
5<?php $wp_list_table->display(); ?>
6
7
8
其实关键的是 $wp_list_table->display(); 这一行代码。
wordpress 的类库 wp_list_table 自始至终都是用来显示数据,例如用户,插件,评论或是文章,这个类库包含了几乎所有的用于显示、排序、分页和搜索的方法。
我们继续追踪下,打开 wp-admin/includes/class-wp-list-table.php 这个文件,找到 display(); 方法:
01 /**
02 * Display the table
03 *
04 * @since 3.1.0
05 * @access public
06 */
07 function display() {
08 extract( $this->_args );
09
10 $this->display_tablenav( 'top' );
11
12?>
13
14
15
16 <?php $this->print_column_headers(); ?>
17
18
19
20
21
22 <?php $this->print_column_headers( false ); ?>
23
24
25
26
>27 <?php $this->display_rows_or_placeholder(); ?>
28
29
30<?php
31 $this->display_tablenav( 'bottom' );
32 }
我们再着眼于生成栏目分类的下面这几行代码:
1
>2 <?php $this->display_rows_or_placeholder(); ?>
3
display_rows_or_placeholder() 这个函数又是怎么回事呢?
01/**
02* Generate the
part of the table03*
04* @since 3.1.0
05* @access protected
06*/
07function display_rows_or_placeholder() {
08 if ( $this->has_items() ) {
09 $this->display_rows();
10 } else {
11 list( $columns, $hidden ) = $this->get_column_info();
12 echo '
';13 $this->no_items();
14 echo '
';15 }
16}
接下来是 has_items() 这个函数,这个函数判断有没有数据需要显示:
01/**
02* Whether the table has items to display or not
03*
04* @since 3.1.0
05* @access public
06*
07* @return bool
08*/
09function has_items() {
10 return !empty( $this->items );
11}
如果有,就 display_rows() :
01/**
02* Generate the table rows
03*
04* @since 3.1.0
05* @access protected
06*/
07function display_rows() {
08 foreach ( $this->items as $item )
09 $this->single_row( $item );
10}
11
12/**
13* Generates content for a single row of the table
14*
15* @since 3.1.0
16* @access protected
17*
18* @param object $item The current item
19*/
20function single_row( $item ) {
21 static $row_class = '';
22 $row_class = ( $row_class == '' ? ' class="alternate"' : '' );
23
24 echo '
';25 $this->single_row_columns( $item );
26 echo '
';27}
28
29/**
30* Generates the columns for a single row of the table
31*
32* @since 3.1.0
33* @access protected
34*
35* @param object $item The current item
36*/
37function single_row_columns( $item ) {
38 list( $columns, $hidden ) = $this->get_column_info();
39
40 foreach ( $columns as $column_name => $column_display_name ) {
41 $class = "class='$column_name column-$column_name'";
42
43 $style = '';
44 if ( in_array( $column_name, $hidden ) )
45 $style = ' style="display:none;"';
46
47 $attributes = "$class$style";
48
49 if ( 'cb' == $column_name ) {
50 echo '
';51 echo $this->column_cb( $item );
52 echo '
';53 }
54 elseif ( method_exists( $this, 'column_' . $column_name ) ) {
55 echo "
";56 echo call_user_func( array( &$this, 'column_' . $column_name ), $item );
57 echo "
";58 }
59 else {
60 echo "
";61 echo $this->column_default( $item, $column_name );
62 echo "
";63 }
64 }
65}
也就是说,根据是否有子栏目先拼凑好栏目分类的 html,再通过 $wp_list_table->display(); 显示到前台。
(责任编辑:最模板)