zoukankan      html  css  js  c++  java
  • WordPress后台edit-tags.php里无限栏目分类实现

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

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

    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(); 方法:

    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() 这个函数,这个函数判断有没有数据需要显示:

    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() :

    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(); 显示到前台。

  • 相关阅读:
    事务四大特征:原子性,一致性,隔离性和持久性(ACID)
    解决“要登录到这台远程计算机,你必须被授予”
    SqlServer_查看SQLServer版本信息
    sed 查找文件的某一行内容
    linux echo命令的-n、-e两个参数
    在.Net中进行跨线程的控件操作(上篇:Control.Invoke)
    .NET性能优化方面的总结
    SQLSERVER2008 显示列信息,包含扩展属性
    C#4.0新特性:可选参数,命名参数,Dynamic
    浅谈.net中的params关键字
  • 原文地址:https://www.cnblogs.com/xiaoyang002/p/4024670.html
Copyright © 2011-2022 走看看