我建造了一个WP_List_Table
显示在我构建的自定义主题页面上。尽管我查看了Internet上关于WP\\U List\\U表格的所有可能信息,但在显示行操作时遇到了一些问题。
任何帮助都会很好!
class Testimonials_List_Table extends WP_List_Table {
function __construct() {
parent::__construct( array(
\'singular\'=> \'testimonial\', //Singular label
\'plural\' => \'testimonials\', //plural label, also this well be one of the table css class
\'ajax\' => false //We won\'t support Ajax for this table
) );
}
function get_bulk_actions() {
$actions = array(
\'delete\' => \'Delete\'
);
return $actions;
}
function process_bulk_action() {
if( \'delete\'===$this->current_action() ) {
wp_die(\'Items deleted (or they would be if we had items to delete)!\');
}
}
function get_columns() {
return $columns= array(
\'cb\' => \'<input type="checkbox" />\', //Render a checkbox instead of text
\'name\'=> \'Name\',
\'company_name\'=> \'Company Name\',
\'company_url\'=> \'Website URL\',
\'testimonials_quote\'=> \'Testimonial\'
);
}
function column_name($item){
$actions = array(
\'edit\' => sprintf(\'<a href="?page=%s&action=%s&testimonial=%s">Edit</a>\',$_REQUEST[\'page\'],\'edit\',$item[\'ID\']),
\'delete\' => sprintf(\'<a href="?page=%s&action=%s&testimonial=%s">Delete</a>\',$_REQUEST[\'page\'],\'delete\',$item[\'ID\']),
);
//Return the title contents
return sprintf(\'%1$s <span style="color:silver">(id:%2$s)</span>%3$s\',
/*$1%s*/ $item[\'name\'],
/*$2%s*/ $item[\'ID\'],
/*$3%s*/ $this->row_actions($actions)
);
}
function column_cb($item){
return sprintf(
\'<input type="checkbox" name="%1$s[]" value="%2$s" />\',
/*$1%s*/ $this->_args[\'singular\'], //Let\'s simply repurpose the table\'s singular label ("movie")
/*$2%s*/ $item[\'ID\'] //The value of the checkbox should be the record\'s id
);
}
function prepare_items() {
global $wpdb, $_wp_column_headers;
$screen = get_current_screen();
$wpdb->show_errors();
$query = "SELECT * FROM wp_testimonials";
$orderby = !empty($_GET["orderby"]) ? mysql_real_escape_string($_GET["orderby"]) : \'ASC\';
$order = !empty($_GET["order"]) ? mysql_real_escape_string($_GET["order"]) : \'\';
if(!empty($orderby) & !empty($order)){ $query.=\' ORDER BY \'.$orderby.\' \'.$order; }
$totalitems = $wpdb->query($query); //return the total number of affected rows
$perpage = 8;
$paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : \'\';
if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
$totalpages = ceil($totalitems/$perpage);
if(!empty($paged) && !empty($perpage)){
$offset=($paged-1)*$perpage;
$query.=\' LIMIT \'.(int)$offset.\',\'.(int)$perpage;
}
$this->set_pagination_args( array(
"total_items" => $totalitems,
"total_pages" => $totalpages,
"per_page" => $perpage,
) );
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $wpdb->get_results($query);
}
function display_rows() {
$records = $this->items;
list( $columns, $hidden ) = $this->get_column_info();
if(!empty($records)){foreach($records as $rec){
echo \'<tr id="record_\'.$rec->name.\'">\';
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;
$editlink = \'/test/wp-admin/themes.php?page=testimonials&tab=add_new_testimonial=\'.urlencode((int)$rec->id);
switch ( $column_name ) {
case "cb": echo \'<td \'.$attributes.\'><input type="checkbox" /></td>\';break;
case "id": echo \'<td \'.$attributes.\'>\'.stripslashes($rec->id).\'</td>\';break;
case "name": echo \'<td \'.$attributes.\'><strong><a href="\'.$editlink.\'" title="Edit">\'.stripslashes($rec->name).\'</a></strong></td>\'; break;
case "company_name": echo \'<td \'.$attributes.\'>\'.stripslashes($rec->company_name).\'</td>\'; break;
case "company_url": echo \'<td \'.$attributes.\'>\'.stripslashes($rec->company_url).\'</td>\'; break;
case "testimonials_quote": echo \'<td \'.$attributes.\'>\'.stripslashes($rec->testimonials_quote).\'</td>\'; break;
}
}
echo\'</tr>\';
}}
}
} // End WP_List_Table class
最合适的回答,由SO网友:Stephen Harris 整理而成
在里面display_rows()
您正在使用以下代码打印“名称”列:
echo \'<td \'.$attributes.\'><strong><a href="\'.$editlink.\'" title="Edit">\'.stripslashes($rec->name).\'</a></strong></td>\';
你哪里都不打电话
column_name()
, 这是您定义的用于显示项目和行操作的函数:
function column_name($item){
$actions = array(
\'edit\' => sprintf(\'<a href="?page=%s&action=%s&testimonial=%s">Edit</a>\',$_REQUEST[\'page\'],\'edit\',$item[\'ID\']),
\'delete\' => sprintf(\'<a href="?page=%s&action=%s&testimonial=%s">Delete</a>\',$_REQUEST[\'page\'],\'delete\',$item[\'ID\']),
);
//Return the title contents
return sprintf(\'%1$s <span style="color:silver">(id:%2$s)</span>%3$s\',
/*$1%s*/ $item[\'name\'],
/*$2%s*/ $item[\'ID\'],
/*$3%s*/ $this->row_actions($actions)
);
}
例如,请参见
WP_List_Table
does it.
事实上,如果您避免过度使用底层类display_rows()
方法,然后它将自动使用column_{$column_id}()
方法(如果存在)来定义列的内容。这使得它比您当前的switch语句更易于维护。