WordPress后台edit-tags.php里无限栏目分类实现
2013-05-01 00:00
633 查看
在 WordPress 里 http://localhost/wordpress3.6.1/wp-admin/edit-tags.php?taxonomy=category 这个链接可以显示 WP 里的无限栏目分类,我们来研究一下 WordPress 是如何实现的。
找到 wp-admin/edit-tags.php 这个文件,发现显示栏目的代码很少:
其实关键的是 $wp_list_table->display(); 这一行代码。
wordpress 的类库 wp_list_table 自始至终都是用来显示数据,例如用户,插件,评论或是文章,这个类库包含了几乎所有的用于显示、排序、分页和搜索的方法。
我们继续追踪下,打开 wp-admin/includes/class-wp-list-table.php 这个文件,找到 display(); 方法:
我们再着眼于生成栏目分类的下面这几行代码:
display_rows_or_placeholder() 这个函数又是怎么回事呢?
接下来是 has_items() 这个函数,这个函数判断有没有数据需要显示:
如果有,就 display_rows() :
也就是说,根据是否有子栏目先拼凑好栏目分类的 html,再通过 $wp_list_table->display(); 显示到前台。
找到 wp-admin/edit-tags.php 这个文件,发现显示栏目的代码很少:
<form id="posts-filter" action="" method="post"> <input type="hidden" name="taxonomy" value="<?php echo esc_attr($taxonomy); ?>" /> <input type="hidden" name="post_type" value="<?php echo esc_attr($post_type); ?>" /> <?php $wp_list_table->display(); ?> <br class="clear" /> </form>
其实关键的是 $wp_list_table->display(); 这一行代码。
wordpress 的类库 wp_list_table 自始至终都是用来显示数据,例如用户,插件,评论或是文章,这个类库包含了几乎所有的用于显示、排序、分页和搜索的方法。
我们继续追踪下,打开 wp-admin/includes/class-wp-list-table.php 这个文件,找到 display(); 方法:
/** * Display the table * * @since 3.1.0 * @access public */ function display() { extract( $this->_args ); $this->display_tablenav( 'top' ); ?> <table class="wp-list-table <?php echo implode( ' ', $this->get_table_classes() ); ?>" cellspacing="0"> <thead> <tr> <?php $this->print_column_headers(); ?> </tr> </thead> <tfoot> <tr> <?php $this->print_column_headers( false ); ?> </tr> </tfoot> <tbody id="the-list"<?php if ( $singular ) echo " data-wp-lists='list:$singular'"; ?>> <?php $this->display_rows_or_placeholder(); ?> </tbody> </table> <?php $this->display_tablenav( 'bottom' ); }
我们再着眼于生成栏目分类的下面这几行代码:
<tbody id="the-list"<?php if ( $singular ) echo " data-wp-lists='list:$singular'"; ?>> <?php $this->display_rows_or_placeholder(); ?> </tbody>
display_rows_or_placeholder() 这个函数又是怎么回事呢?
/** * Generate the <tbody> part of the table * * @since 3.1.0 * @access protected */ function display_rows_or_placeholder() { if ( $this->has_items() ) { $this->display_rows(); } else { list( $columns, $hidden ) = $this->get_column_info(); echo '<tr class="no-items"><td class="colspanchange" colspan="' . $this->get_column_count() . '">'; $this->no_items(); echo '</td></tr>'; } }
接下来是 has_items() 这个函数,这个函数判断有没有数据需要显示:
/** * Whether the table has items to display or not * * @since 3.1.0 * @access public * * @return bool */ function has_items() { return !empty( $this->items ); }
如果有,就 display_rows() :
/** * Generate the table rows * * @since 3.1.0 * @access protected */ function display_rows() { foreach ( $this->items as $item ) $this->single_row( $item ); } /** * Generates content for a single row of the table * * @since 3.1.0 * @access protected * * @param object $item The current item */ function single_row( $item ) { static $row_class = ''; $row_class = ( $row_class == '' ? ' class="alternate"' : '' ); echo '<tr' . $row_class . '>'; $this->single_row_columns( $item ); echo '</tr>'; } /** * Generates the columns for a single row of the table * * @since 3.1.0 * @access protected * * @param object $item The current item */ function single_row_columns( $item ) { list( $columns, $hidden ) = $this->get_column_info(); foreach ( $columns as $column_name => $column_display_name ) { $class = "class='$column_name column-$column_name'"; $style = ''; if ( in_array( $column_name, $hidden ) ) $style = ' style="display:none;"'; $attributes = "$class$style"; if ( 'cb' == $column_name ) { echo '<th scope="row" class="check-column">'; echo $this->column_cb( $item ); echo '</th>'; } elseif ( method_exists( $this, 'column_' . $column_name ) ) { echo "<td $attributes>"; echo call_user_func( array( &$this, 'column_' . $column_name ), $item ); echo "</td>"; } else { echo "<td $attributes>"; echo $this->column_default( $item, $column_name ); echo "</td>"; } } }
也就是说,根据是否有子栏目先拼凑好栏目分类的 html,再通过 $wp_list_table->display(); 显示到前台。
相关文章推荐
- WordPress后台edit-tags.php里无限栏目分类实现
- WordPress后台edit-tags.php里无限栏目分类实现
- 无限级分类在edit方法下,实现上级栏目选中
- php 用递归实现的无限级别分类
- php 递归 实现无限分类 格式化数组
- PHP迭代与递归实现无限级分类
- php利用递归函数实现无限级分类
- PHP全路径无限分类导航LINK代码实现
- PHP进阶教程:实现网站的无限分类
- PHP递归遍历多维数组实现无限分类的方法
- php 递归 实现无限分类 格式化数组
- Asp.Net实现无限分类生成表格的方法(后台自定义输出table)
- Asp.Net实现无限分类生成表格的方法(后台自定义输出table)
- PHP实现无限分类
- php递归实现无限分类生成下拉列表的函数
- 用PHP实现无限级分类详解
- PHP + MySQL 实现无限分类的2种方法
- 帖几个PHP的无限分类实现想法~
- 一个好用的php 实现无限分类的方法