admin管理员组文章数量:1122832
I'm trying to add an admin style table in a regular page with a sample below. The code is inside the theme's functions.php.
It seems the class is not loaded and crashes so the echo after is not called.
I have no problems creating tables as a plugin and inside an admin section, but trying to do it in a regular page.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class Transactions_List_Table extends WP_List_Table {
}
function render_transactions_list_page() {
$transactions_table = new Transactions_List_Table();
echo 'Hello';
}
// Add this to a shortcode if you want to display it on a regular page
add_shortcode( 'transactions_table', 'render_transactions_list_page' );
I'm trying to add an admin style table in a regular page with a sample below. The code is inside the theme's functions.php.
It seems the class is not loaded and crashes so the echo after is not called.
I have no problems creating tables as a plugin and inside an admin section, but trying to do it in a regular page.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class Transactions_List_Table extends WP_List_Table {
}
function render_transactions_list_page() {
$transactions_table = new Transactions_List_Table();
echo 'Hello';
}
// Add this to a shortcode if you want to display it on a regular page
add_shortcode( 'transactions_table', 'render_transactions_list_page' );
Share
Improve this question
edited Sep 21, 2024 at 7:32
ruzip
asked Sep 21, 2024 at 7:31
ruzipruzip
11 bronze badge
2
- Why 4.x? that's out of date and insecure. – Chris Cox Commented Sep 21, 2024 at 10:23
- Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Bot Commented Sep 23, 2024 at 22:23
1 Answer
Reset to default 0We need to made few changes in the code so that it works properly. Here is the updated code.
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
class Transactions_List_Table extends WP_List_Table {
public function __construct() {
parent::__construct( array(
'singular' => 'transaction',
'plural' => 'transactions',
'ajax' => false,
) );
}
// Here are preparing our items.
public function prepare_items() {
// Here we would normally retrieve data from the database.
$this->items = array(
array( 'id' => 1, 'name' => 'Transaction 1', 'amount' => '$100' ),
array( 'id' => 2, 'name' => 'Transaction 2', 'amount' => '$200' ),
);
$this->_column_headers = array( $this->get_columns(), array(), array() );
}
public function get_columns() {
return array(
'id' => __( 'ID', 'textdomain' ),
'name' => __( 'Transaction Name', 'textdomain' ),
'amount' => __( 'Amount', 'textdomain' ),
);
}
public function column_default( $item, $column_name ) {
switch ( $column_name ) {
case 'id':
case 'name':
case 'amount':
return $item[ $column_name ];
default:
return '';
}
}
public function display() {
echo '<div class="wrap">';
echo '<h2>' . __( 'Transactions', 'textdomain' ) . '</h2>';
parent::display();
echo '</div>';
}
}
function render_transactions_list_page() {
$transactions_table = new Transactions_List_Table();
$transactions_table->prepare_items();
ob_start();
$transactions_table->display();
return ob_get_clean();
}
// We have added this to a shortcode to display it on a regular page.
add_shortcode( 'transactions_table', 'render_transactions_list_page' );
本文标签: Adding a Admin Style Table in a Wordpress 4x Page
版权声明:本文标题:Adding a Admin Style Table in a Wordpress 4.x Page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736290102a1928442.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论