🥰Một ví dụ tuyệt vời về phần trang sử dụng WP_List_Table (ok)
Last updated
Last updated
C:\xampp82\htdocs\wp2\wp-content\themes\twentytwentyfour\page-test.php
<?php
echo do_shortcode('[bxft-table]');
add_shortcode( 'bxft-table', 'bxft_table_shortcode' );
function bxft_table_shortcode( $atts ) {
ob_start();
include_once 'class-bxft-table.php';
$template = ob_get_contents();
ob_end_clean();
return $template;
}
C:\xampp82\htdocs\wp2\wp-content\themes\twentytwentyfour\class-bxft-table.php
<?php
if (!class_exists('WP_List_Table')) {
require_once ABSPATH . 'wp-admin/includes/template.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-screen.php';
require_once ABSPATH . 'wp-admin/includes/screen.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class BXFT_Table extends WP_List_Table
{
public function prepare_items()
{
global $wp;
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$data = $this->table_data();
// usort($data, array(&$this, 'sort_data'));
$perPage = 2;
$currentPage = $this->get_pagenum();
$tokens = explode('/', $wp->request);
if (count($tokens) == 3) :
$currentPage = $tokens[2];
endif;
$totalItems = count($data);
$data = array_slice($data, (($currentPage - 1) * $perPage), $perPage);
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $data;
$pagerange = 2;
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => ceil($totalItems / $perPage),
'current' => $currentPage,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$page_links = paginate_links($pagination_args);
if ($page_links) {
echo '<div class="tablenav"><div class="tablenav-pages" style="margin: 1em 0">' .
$page_links . '</div></div>';
}
}
/**
* Override the parent columns method. Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns()
{
$columns = array(
'id' => 'ID',
'title' => 'Title',
'description' => 'Description',
'year' => 'Year',
'director' => 'Director',
'rating' => 'Rating'
);
return $columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns()
{
return array();
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns()
{
return array('title' => array('title', false));
}
/**
* Get the table data
*
* @return Array
*/
private function table_data()
{
$data = array();
$data[] = array(
'id' => 1,
'title' => 'The Shawshank Redemption',
'description' => 'Two imprisoned men bond over a number of years, finding solace and eventual redemption through acts of common decency.',
'year' => '1994',
'director' => 'Frank Darabont',
'rating' => '9.3'
);
$data[] = array(
'id' => 2,
'title' => 'The Godfather',
'description' => 'The aging patriarch of an organized crime dynasty transfers control of his clandestine empire to his reluctant son.',
'year' => '1972',
'director' => 'Francis Ford Coppola',
'rating' => '9.2'
);
$data[] = array(
'id' => 3,
'title' => 'The Godfather: Part II',
'description' => 'The early life and career of Vito Corleone in 1920s New York is portrayed while his son, Michael, expands and tightens his grip on his crime syndicate stretching from Lake Tahoe, Nevada to pre-revolution 1958 Cuba.',
'year' => '1974',
'director' => 'Francis Ford Coppola',
'rating' => '9.0'
);
$data[] = array(
'id' => 4,
'title' => 'Pulp Fiction',
'description' => 'The lives of two mob hit men, a boxer, a gangster\'s wife, and a pair of diner bandits intertwine in four tales of violence and redemption.',
'year' => '1994',
'director' => 'Quentin Tarantino',
'rating' => '9.0'
);
$data[] = array(
'id' => 5,
'title' => 'The Good, the Bad and the Ugly',
'description' => 'A bounty hunting scam joins two men in an uneasy alliance against a third in a race to find a fortune in gold buried in a remote cemetery.',
'year' => '1966',
'director' => 'Sergio Leone',
'rating' => '9.0'
);
$data[] = array(
'id' => 6,
'title' => 'The Dark Knight',
'description' => 'When Batman, Gordon and Harvey Dent launch an assault on the mob, they let the clown out of the box, the Joker, bent on turning Gotham on itself and bringing any heroes down to his level.',
'year' => '2008',
'director' => 'Christopher Nolan',
'rating' => '9.0'
);
$data[] = array(
'id' => 7,
'title' => '12 Angry Men',
'description' => 'A dissenting juror in a murder trial slowly manages to convince the others that the case is not as obviously clear as it seemed in court.',
'year' => '1957',
'director' => 'Sidney Lumet',
'rating' => '8.9'
);
$data[] = array(
'id' => 8,
'title' => 'Schindler\'s List',
'description' => 'In Poland during World War II, Oskar Schindler gradually becomes concerned for his Jewish workforce after witnessing their persecution by the Nazis.',
'year' => '1993',
'director' => 'Steven Spielberg',
'rating' => '8.9'
);
$data[] = array(
'id' => 9,
'title' => 'The Lord of the Rings: The Return of the King',
'description' => 'Gandalf and Aragorn lead the World of Men against Sauron\'s army to draw his gaze from Frodo and Sam as they approach Mount Doom with the One Ring.',
'year' => '2003',
'director' => 'Peter Jackson',
'rating' => '8.9'
);
$data[] = array(
'id' => 10,
'title' => 'Fight Club',
'description' => 'An insomniac office worker looking for a way to change his life crosses paths with a devil-may-care soap maker and they form an underground fight club that evolves into something much, much more...',
'year' => '1999',
'director' => 'David Fincher',
'rating' => '8.8'
);
$data[] = array(
'id' => 11,
'title' => 'Fight Club 11',
'description' => 'An insomniac office worker looking for a way to change his life crosses paths with a devil-may-care soap maker and they form an underground fight club that evolves into something much, much more...',
'year' => '1999',
'director' => 'David Fincher',
'rating' => '8.8'
);
return $data;
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default($item, $column_name)
{
switch ($column_name) {
case 'id':
case 'title':
case 'description':
case 'year':
case 'director':
case 'rating':
return $item[$column_name];
default:
return print_r($item, true);
}
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @return Mixed
*/
private function sort_data($a, $b)
{
// Set defaults
$orderby = 'id';
$order = 'asc';
// If orderby is set, use this as the sort column
if (!empty($_GET['orderby'])) {
$orderby = $_GET['orderby'];
}
// If order is set use this as the order
if (!empty($_GET['order'])) {
$order = $_GET['order'];
}
$result = strcmp($a[$orderby], $b[$orderby]);
if ($order === 'asc') {
return $result;
}
return - $result;
}
}
function display_bxft_table()
{
$bxft_table = new BXFT_Table();
$bxft_table->prepare_items();
?>
<div class="wrap">
<?php $bxft_table->display(); ?>
</div>
<?php
}
display_bxft_table();
Hoàn thành (Cần plugin Contact Form CFDB7)
C:\xampp82\htdocs\wp2\wp-content\themes\twentytwentyfour\functions.php
add_shortcode( 'bxft-table', 'bxft_table_shortcode' );
function bxft_table_shortcode( $atts ) {
ob_start();
include_once 'class-bxft-table.php';
$template = ob_get_contents();
ob_end_clean();
return $template;
}
C:\xampp82\htdocs\wp2\wp-content\themes\twentytwentyfour\page-test.php
<div class="tablenav__wrap">
<?php
echo do_shortcode('[bxft-table]');
?>
</div>
C:\xampp82\htdocs\wp2\wp-content\themes\twentytwentyfour\class-bxft-table.php
<?php
if (!class_exists('WP_List_Table')) {
require_once ABSPATH . 'wp-admin/includes/template.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-screen.php';
require_once ABSPATH . 'wp-admin/includes/screen.php';
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class BXFT_Table extends WP_List_Table
{
public $i = 0;
public function prepare_items()
{
global $wp;
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$data = $this->wp_list_table_data();
// usort($data, array(&$this, 'sort_data'));
$perPage = 5;
$currentPage = $this->get_pagenum();
$tokens = explode('/', $wp->request);
if (count($tokens) == 3) :
$currentPage = $tokens[2];
endif;
$totalItems = count($data);
$data = array_slice($data, (($currentPage - 1) * $perPage), $perPage);
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = $data;
$pagerange = 2;
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => ceil($totalItems / $perPage),
'current' => $currentPage,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$page_links = paginate_links($pagination_args);
if ($page_links) {
echo '<div class="tablenav"><div class="tablenav-pages">' . $page_links . '</div></div>';
}
}
/**
* Override the parent columns method. Defines the columns to use in your listing table
*
* @return Array
*/
public function get_columns()
{
$columns = array(
'form_id' => 'No',
'title' => 'Title',
'writer' => 'Writer',
'form-date' => 'Date',
'rating' => 'Rating'
);
return $columns;
}
/**
* Define which columns are hidden
*
* @return Array
*/
public function get_hidden_columns()
{
return array();
}
/**
* Define the sortable columns
*
* @return Array
*/
public function get_sortable_columns()
{
// return array('title' => array('title', false));
}
public function wp_list_table_data()
{
$data = array();
global $wpdb;
$cfdb = apply_filters('cfdb7_database', $wpdb);
$table_name = $cfdb->prefix . 'db7_forms';
$orderby = isset($_GET['orderby']) ? 'form_date' : 'form_id';
$results = $cfdb->get_results("SELECT * FROM $table_name WHERE form_post_id = 5", OBJECT);
foreach ($results as $result) {
$form_value = unserialize($result->form_value);
$link = "%s";
$fid = $result->form_post_id;
$form_values['form_id'] = $result->form_id;
$column_titles = array();
foreach ($column_titles as $col_title) {
$form_value[$col_title] = isset($form_value[$col_title]) ?
$form_value[$col_title] : '';
}
foreach ($form_value as $k => $value) {
$ktmp = $k;
$can_foreach = is_array($value) || is_object($value);
if ($can_foreach) {
foreach ($value as $k_val => $val) :
$val = esc_html($val);
$form_values[$ktmp] = (strlen($val) > 150) ? substr($val, 0, 150) . '...' : $val;
$form_values[$ktmp] = sprintf($link, $form_values[$ktmp]);
endforeach;
} else {
$value = esc_html($value);
$form_values[$ktmp] = (strlen($value) > 150) ? substr($value, 0, 150) . '...' : $value;
$form_values[$ktmp] = sprintf($link, $form_values[$ktmp]);
}
}
$form_values['form-date'] = sprintf($link, $result->form_date);
$data[] = $form_values;
}
return $data;
}
// Hide Show more details
protected function single_row_columns($item)
{
global $wp;
list($columns, $hidden, $sortable, $primary) = $this->get_column_info();
$currentPage = $this->get_pagenum();
$tokens = explode('/', $wp->request);
if (count($tokens) == 3) :
$currentPage = $tokens[2];
endif;
$this->i++;
foreach ($columns as $column_name => $column_display_name) {
$classes = "$column_name column-$column_name";
if ($primary === $column_name) {
$classes .= ' has-row-actions column-primary';
}
if (in_array($column_name, $hidden, true)) {
$classes .= ' hidden';
}
/*
* Comments column uses HTML in the display name with screen reader text.
* Strip tags to get closer to a user-friendly string.
*/
$data = 'data-colname="' . esc_attr(wp_strip_all_tags($column_display_name)) . '"';
$attributes = "class='$classes' $data";
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 call_user_func(
array($this, '_column_' . $column_name),
$item,
$classes,
$data,
$primary
);
} elseif (method_exists($this, 'column_' . $column_name)) {
echo "<td $attributes>";
echo call_user_func(array($this, 'column_' . $column_name), $item);
echo $this->handle_row_actions($item, $column_name, $primary);
echo '</td>';
} else {
echo "<td $attributes>";
echo $this->column_default($item, $column_name, $this->i);
// echo $this->handle_row_actions( $item, $column_name, $primary );
echo '</td>';
}
}
}
/**
* Define what data to show on each column of the table
*
* @param Array $item Data
* @param String $column_name - Current column name
*
* @return Mixed
*/
public function column_default($item, $column_name, $i = 1)
{
switch ($column_name) {
case 'form_id':
return $i;
case 'title':
case 'writer':
case 'form-date':
case 'rating':
return $item[$column_name];
default:
return print_r($item, true);
}
}
/**
* Allows you to sort the data by the variables set in the $_GET
*
* @return Mixed
*/
private function sort_data($a, $b)
{
// Set defaults
$orderby = 'form_id';
$order = 'asc';
// If orderby is set, use this as the sort column
if (!empty($_GET['orderby'])) {
$orderby = $_GET['orderby'];
}
// If order is set use this as the order
if (!empty($_GET['order'])) {
$order = $_GET['order'];
}
$result = strcmp($a[$orderby], $b[$orderby]);
if ($order === 'asc') {
return $result;
}
return -$result;
}
// == Hide tfoot
public function display()
{
$singular = $this->_args['singular'];
$this->display_tablenav('top');
$this->screen->render_screen_reader_content('heading_list');
?>
<table class="wp-list-table <?php echo implode(' ', $this->get_table_classes()); ?>">
<?php $this->print_table_description(); ?>
<thead>
<tr>
<?php $this->print_column_headers(); ?>
</tr>
</thead>
<tbody id="the-list">
<?php $this->display_rows_or_placeholder(); ?>
</tbody>
</table>
<?php
$this->display_tablenav('bottom');
}
}
function display_bxft_table()
{
$bxft_table = new BXFT_Table();
$bxft_table->prepare_items();
?>
<div class="wrap">
<?php $bxft_table->display(); ?>
</div>
<?php
}
display_bxft_table();