您的位置:首页 > 编程语言 > PHP开发

WordPress后台edit-tags.php里无限栏目分类实现

2014-10-14 16:23 423 查看
在WordPress里http://localhost/wordpress3.6.1/wp-admin/edit-tags.php?taxonomy=category这个链接可以显示WP里的无限栏目分类,我们来研究一下WordPress是如何实现的。

找到wp-admin/edit-tags.php这个文件,发现显示栏目的代码很少:

viewsource

print?

1
<formid=
"posts-filter"
action=
""
method=
"post"
>
2
<inputtype=
"hidden"
name=
"taxonomy"
value=
"<?phpechoesc_attr($taxonomy);?>"
/>
3
<inputtype=
"hidden"
name=
"post_type"
value=
"<?phpechoesc_attr($post_type);?>"
/>
4
5
<?php
$wp_list_table
->display();?>
6
7
<br
class
=
"clear"
/>
8
</form>
其实关键的是$wp_list_table->display();这一行代码。

wordpress的类库wp_list_table自始至终都是用来显示数据,例如用户,插件,评论或是文章,这个类库包含了几乎所有的用于显示、排序、分页和搜索的方法。

我们继续追踪下,打开wp-admin/includes/class-wp-list-table.php这个文件,找到display();方法:

viewsource

print?

01
/**
02
*Displaythetable
03
*
04
*@since3.1.0
05
*@accesspublic
06
*/
07
function
display(){
08
extract(
$this
->_args);
09
10
$this
->display_tablenav(
'top'
);
11
12
?>
13
<table
class
=
"wp-list-table<?phpechoimplode('',$this->get_table_classes());?>"
cellspacing=
"0"
>
14
<thead>
15
<tr>
16
<?php
$this
->print_column_headers();?>
17
</tr>
18
</thead>
19
20
<tfoot>
21
<tr>
22
<?php
$this
->print_column_headers(false);?>
23
</tr>
24
</tfoot>
25
26
<tbodyid=
"the-list"
<?php
if
(
$singular
)
echo
"data-wp-lists='list:$singular'"
;?>>
27
<?php
$this
->display_rows_or_placeholder();?>
28
</tbody>
29
</table>
30
<?php
31
$this
->display_tablenav(
'bottom'
);
32
}
我们再着眼于生成栏目分类的下面这几行代码:

viewsource

print?

1
<tbodyid=
"the-list"
<?php
if
(
$singular
)
echo
"data-wp-lists='list:$singular'"
;?>>
2
<?php
$this
->display_rows_or_placeholder();?>
3
</tbody>
display_rows_or_placeholder()这个函数又是怎么回事呢?

viewsource

print?

01
/**
02
*Generatethe<tbody>partofthetable
03
*
04
*@since3.1.0
05
*@accessprotected
06
*/
07
function
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
'<trclass="no-items"><tdclass="colspanchange"colspan="'
.
$this
->get_column_count().
'">'
;
13
$this
->no_items();
14
echo
'</td></tr>'
;
15
}
16
}
接下来是has_items()这个函数,这个函数判断有没有数据需要显示:

viewsource

print?

01
/**
02
*Whetherthetablehasitemstodisplayornot
03
*
04
*@since3.1.0
05
*@accesspublic
06
*
07
*@returnbool
08
*/
09
function
has_items(){
10
return
!
empty
(
$this
->items);
11
}
如果有,就display_rows():

viewsource

print?

01
/**
02
*Generatethetablerows
03
*
04
*@since3.1.0
05
*@accessprotected
06
*/
07
function
display_rows(){
08
foreach
(
$this
->items
as
$item
)
09
$this
->single_row(
$item
);
10
}
11
12
/**
13
*Generatescontentforasinglerowofthetable
14
*
15
*@since3.1.0
16
*@accessprotected
17
*
18
*@paramobject$itemThecurrentitem
19
*/
20
function
single_row(
$item
){
21
static
$row_class
=
''
;
22
$row_class
=(
$row_class
==
''
?
'class="alternate"'
:
''
);
23
24
echo
'<tr'
.
$row_class
.
'>'
;
25
$this
->single_row_columns(
$item
);
26
echo
'</tr>'
;
27
}
28
29
/**
30
*Generatesthecolumnsforasinglerowofthetable
31
*
32
*@since3.1.0
33
*@accessprotected
34
*
35
*@paramobject$itemThecurrentitem
36
*/
37
function
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_namecolumn-$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
'<thscope="row"class="check-column">'
;
51
echo
$this
->column_cb(
$item
);
52
echo
'</th>'
;
53
}
54
elseif
(method_exists(
$this
,
'column_'
.
$column_name
)){
55
echo
"<td$attributes>"
;
56
echo
call_user_func(
array
(&
$this
,
'column_'
.
$column_name
),
$item
);
57
echo
"</td>"
;
58
}
59
else
{
60
echo
"<td$attributes>"
;
61
echo
$this
->column_default(
$item
,
$column_name
);
62
echo
"</td>"
;
63
}
64
}
65
}
也就是说,根据是否有子栏目先拼凑好栏目分类的html,再通过$wp_list_table->display();显示到前台。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: