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

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

view source

 

print?

1 <form id="posts-filter" action="" method="post">
2 <input type="hidden" name="taxonomy" value="<?php echo esc_attr($taxonomy); ?>" />
3 <input type="hidden" name="post_type" value="<?php echo esc_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(); 方法:

view source

 

print?

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 <table class="wp-list-table <?php echo implode( ' ', $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     <tbody id="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     }

我们再着眼于生成栏目分类的下面这几行代码:

1 <tbody id="the-list"<?php if ( $singular ) echo " data-wp-lists='list:$singular'"; ?>>
2     <?php $this->display_rows_or_placeholder(); ?>
3 </tbody>

display_rows_or_placeholder() 这个函数又是怎么回事呢?

01 /**
02 * Generate the <tbody> part of the table
03 *
04 * @since 3.1.0
05 * @access protected
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 '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">';
13         $this->no_items();
14         echo '</td></tr>';
15     }
16 }

接下来是 has_items() 这个函数,这个函数判断有没有数据需要显示:

view source

 

print?

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 */
09 function has_items() {
10     return !empty( $this->items );
11 }

如果有,就 display_rows() :

view source

 

print?

01 /**
02 * Generate the table rows
03 *
04 * @since 3.1.0
05 * @access protected
06 */
07 function 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 */
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 * 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 */
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_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 '<th scope="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(); 显示到前台。

WordPress后台edit-tags.php里无限栏目分类实现的更多相关文章

  1. PHP的无限栏目分类

    自己在PHP的无线栏目分类上面就是搞了很久都没有明白,所以现在是趁着记忆力还没有完全的消退的时候速度的记录下来 这里讲解的是最简单的树形栏目,适合的是小中型的栏目分类需求 1.这里讲解的是针对是只要通 ...

  2. 黄聪:WordPress 后台发布文章时提示用户选择分类

    很多用户在后台发布文章,常常会忘记选择分类,所以很有必要添加一个提醒功能,如果没有选择分类,点击发布时,就显示一个提示信息.要实现这个功能,只要将下面的代码添加到主题的 functions.php 即 ...

  3. destoon7.0后台栏目分类一键获取所有栏目拼音目录

    近期研究DT,从DT4.0一直研究到DT7.0,总算也有些心得.最近重新开发设计了一个信息资讯站点:http://www.xuetong365.com/ 废话不多说,上教程 用于DESTOON7.0系 ...

  4. WordPress后台的文章、分类,媒体,页面,评论,链接等所有信息中显示ID并将ID设置为第一列

    WordPress后台默认是不显示文章.分类等信息ID的,查看起来非常不方便,不知道Wp团队出于什么原因默认不显示这个但可以使用Simply Show IDs插件来实现 不使用插件,其他网友的实现: ...

  5. 黄聪:定制化WordPress后台自定义仪表盘

    WordPress作为一博客管理系统,相对来说已经相当简洁了,对用户也十分友好,新手也极易上手. 仪表盘是我们登陆WordPress后看到的后台界面,映入眼帘的是各种各样的信息,如WordPress ...

  6. WordPress的后台功能菜单介绍与操作,WordPress后台说明

    WordPress网站的后台概况和登陆地址 网站都有个后台管理系统,通过网站后台,你可以改变你的网站外观,管理你网站的数据,给网站前台增加页面,文章,视频,图片或者其他功能. 通过WordPress建 ...

  7. 解决WordPress后台安装主题、插件图片不显示的问题

    今天搭建wordpress发现现在主题的时候预览图片都没有了,于是搜索了一下,发现下面的这个方法确实管用,于是转载收藏. 有在WordPress后台安装主题.插件的小伙伴可能会遇到主题.插件图片不显示 ...

  8. 如何通过数据库修改WordPress后台登录密码

    大家是否有过因为忘记WordPress后台登陆密码的时候?其实WordPress后台登陆密码的找回或修改的方法有多种,比如通过邮箱重启密码,又或者通过主机控制面板进入数据库修改等等.本篇教程以GoDd ...

  9. wordpress后台进去空白怎么办?

    最近博客换成了用wordpress程序搭建,内容和版面也重新设计.经常使用FTP工具,更改模板或者其他程序文件.由于对wordpress不太了解,竟然出现了wordpress后台进去空白的问题,而前台 ...

随机推荐

  1. 关于JUnit4无法支持多线程测试的解决方法

    转自:https://segmentfault.com/a/1190000003762719 其实junit是将test作为参数传递给了TestRunner的main函数.并通过main函数进行执行. ...

  2. codeblocks编译出错问题的解答!(编译c++ 或者c程序)

    典型错误:https://blog.csdn.net/jingmiaa/article/details/52054204 MinGW下载并配置gcc/g++编译环境:https://blog.csdn ...

  3. Linux 下 PHP 扩展 PDO 编译安装

    1.进入 PHP 的软件包 pdo 扩展目录中(注:不是 PHP 安装目录) [root@tester /]# /home/tdweb/php-5.4.34/ext/pdo_mysql 执行 phpi ...

  4. iOS下原生与JS交互(总结)

    iOS开发免不了要与UIWebView打交道,然后就要涉及到JS与原生OC交互,今天总结一下JS与原生OC交互的两种方式. JS调用原生OC篇(我自己用的方式二,简单方便) 方式一 第一种方式是用JS ...

  5. Selenium搭配TestNG

    用Maven来构建TestNG依赖: <dependency> <groupId>org.testng</groupId> <artifactId>te ...

  6. 如何用Fiddler 拦住RestAssured发出的请求

    用RestAssured 发出的请求并不能直接被fiddler 拦截,可以在初始化的时候做出如下配置: RestAssured.proxy("localhost", 8888); ...

  7. Wordpress 从 MySQL 获取文章链接 permalinks

    SELECT wpp.post_title, wpp.guid, wpp.post_date, REPLACE( REPLACE( REPLACE( REPLACE( wpo.option_value ...

  8. LightGBM详细用法--机器学习算法--周振洋

    LightGBM算法总结 2018年08月21日 18:39:47 Ghost_Hzp 阅读数:2360 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.ne ...

  9. 时间动态协同过滤(TimeSVD++)

    原作者 原论文地址 http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.379.1951&rep=rep1&type=pd ...

  10. 论文翻译 - Multiagent Bidirectionally-Coordinated Nets Emergence of Human-level Coordination in Learning to Play StarCraft Combat Games

    (缺少一些公式的图或者效果图,评论区有惊喜) (个人学习这篇论文时进行的翻译[谷歌翻译,你懂的],如有侵权等,请告知) Multiagent Bidirectionally-Coordinated N ...