저는 WordPress에 처음 왔어요. WP_List_Table
클래스를 사용하여 WordPress 테이블을 만들려고합니다. 나는 테이블을 만들었지 만 시간이 오래 걸린다. 그래서 WordPress 테이블을 만들 수있는 함수를 만들고 싶습니다. 여기서는 함수에 데이터와 열 배열을 전달할 수 있으며 그 함수는 필요한 WordPress 테이블을 만듭니다. 편집, 삭제 및 정렬 가능한 기능으로 테이블을 만들고 싶습니다.`WP_List_Table`을 사용하여 테이블을 만드는 사용자 정의 함수?
1
A
답변
2
이 코드를 사용하면 역동적 인 함수가되지만 첫 번째 인자 케이를 전달해야하며 name은 id입니다.
이것은 동적 인 내 클래스 인 WP_List_table입니다.
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of wplist_table
*
* @author renishkhunt
*/
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
class wplist_table extends WP_List_Table
{
//put your code here
var $data = array();
var $default_columns = array();
public function wplist_table($datad, $columns)
{
parent::__construct();
$this->data = $datad;
$this->default_columns = $columns;
}
function get_columns()
{
return $this->default_columns;
}
function prepare_items()
{
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
$this->_column_headers = array($columns, $hidden, $sortable);
usort($this->data, array(&$this, 'usort_recorder'));
$per_page = 10;
$current_page = $this->get_pagenum();
$total_items = count($this->data);
// only ncessary because we have sample data
$this->found_data = array_slice($this->data, (($current_page - 1) * $per_page), $per_page);
$this->set_pagination_args(array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page //WE have to determine how many items to show on a page
));
$this->items = $this->found_data;
}
function column_default($item, $column_name)
{
foreach ($this->default_columns as $keys => $values) {
if ($values == $column_name) {
if(isset($item[$column_name])){
return $item[$column_name];
}
}
}
}
function get_sortable_columns()
{
$i=0;
$sortables = array();
foreach ($this->default_columns as $keys => $values) {
if($i == 0){
$i++;
//continue;
}
$sortables[$keys] = array($values,false);
}
return $sortables;
}
function usort_recorder($a, $b)
{
$orderby = (!empty($_GET['orderby'])) ? $_GET['orderby'] : 'id';
$order = (!empty($_GET['order'])) ? $_GET['order'] : 'asc';
$resutl = strcmp($a[$orderby], $b[$orderby]);
return ($order === 'asc') ? $resutl : -$resutl;
}
function column_Name($item)
{
$action = array(
'edit' => sprintf('<a href="?page=%s&action=%s&fields=%s">Edit</a>', $_REQUEST['page'], 'edit', $item['id']),
'delete' => sprintf('<a href="?page=%s&action=%s&fields=%s">Delete</a>', $_REQUEST['page'], 'delete', $item['id'])
);
return sprintf('%1$s %2$s', $item['name'], $this->row_actions($action));
}
function get_bulk_action()
{
$actions = array(
'delete' => 'Delete '
);
return $actions;
}
function column_db($item)
{
return sprintf("<input type='checkbox' name='id[]' value='%s'", $item['id']);
}
}
?>
파일에서 해당 코드를 복사하고이 같은 열 이름 및 데이터와 같은 인수를 전달하기 만하면됩니다.
$data = array(
array("id" => 1, "name" => "Renish Khunt", "add" => "asd"),
array("id" => 2, "name" => "Renish Khunt", "add" => "asd"),
array("id" => 3, "name" => "Renish Khunt", "add" => "asd")
);
$columns = array(
"name" => "name",
"add" => "add"
);
그런 다음 클래스 객체를 만들고 두 개의 인수에 다음과 같은 데이터와 열 이름을 전달하십시오. 나는 이것이 당신이 완벽하게 사용되기를 기대
$mylist_table = new wplist_table($data, $columns);
echo '<div class="wrap"><h2>Custome Fields<a class="add-new-h2" href="?page=' . $_REQUEST['page'] . '&action=add">Add New</a></h2>';
$mylist_table->prepare_items();
$mylist_table->display();
echo "</div>";
는 열 이름과 $ 데이터 배열을 추가 $ 열 배열에 더 열을 표시 같은 키 또는 값으로 컬럼의 이름을 추가 할 필요가 동적 클래스입니다.
$data = array(
array("id" => 1, "name" => "Renish Khunt", "add" => "asd","newcolumn"=>"value"),
array("id" => 2, "name" => "Renish Khunt", "add" => "asd","newcolumn"=>"value"),
array("id" => 3, "name" => "Renish Khunt", "add" => "asd","newcolumn"=>"value")
);
$columns = array(
"name" => "name",
"add" => "add",
"newcolumn"=>"New Column"
);
이 코드가 마음에 들었다.
감사합니다.
관련 문제
- 1. WordPress에 사용자 정의 드롭 다운 필터를 추가하는 방법 WP_List_Table
- 2. 테이블 너비 WP_List_Table 줄이기
- 3. 테이블을 만드는 사용자 방지
- 4. C#을 사용하여 Excel에서 사용자 정의 함수 개발?
- 5. edmgen.exe 사용자 정의 함수
- 6. 사용자 정의 집계 함수
- 7. R + 사용자 정의 함수
- 8. MySQL의 사용자 정의 함수
- 9. 보기 또는 사용자 정의 함수?
- 10. 는 사용자 정의 함수
- 11. 사용자 정의 함수
- 12. 난수로 SQL 사용자 정의 함수
- 13. 사용자 정의 함수 만들기 edmx에서
- 14. Wordpress에서 WP_List_Table 코드로 검색하기
- 15. jquery ajax를 사용하여 컨텐츠를로드하는 사용자 정의 함수
- 16. Matlab의 : 호출 사용자 정의 함수
- 17. SQL 사용자 정의 함수
- 18. 사용자 정의 함수
- 19. 사용자 정의 함수
- 20. jQuery 함수 사용자 정의
- 21. jQuery 사용자 정의 함수
- 22. 그래프 사용자 정의 함수
- 23. 사용자 정의 함수
- 24. MySQL 사용자 정의 함수
- 25. 스레드의 사용자 정의 함수
- 26. Vim의 사용자 정의 함수
- 27. MySQL : 사용자 정의 함수
- 28. SQL 사용자 정의 함수 질문
- 29. node.js를 사용하여 사용자 정의 객체를 만드는 방법
- 30. dplyr의 R 사용자 정의 함수
http://codingbin.com/display-custom-table-data-wordpress-admin/에서 확인하십시오. – MKD