0

How to display data using WP_List_Table ? My code is like below

class myTable extends WP_List_Table
{
    var $example_data = array(
        array('ID' => 1,'booktitle' => 'Quarter Share', 'author' => 'Nathan Lowell',
                'isbn' => '978-0982514542'),
        array('ID' => 2, 'booktitle' => '7th Son: Descent','author' => 'J. C. Hutchins',
                'isbn' => '0312384378'),
        array('ID' => 3, 'booktitle' => 'Shadowmagic', 'author' => 'John Lenahan',
                'isbn' => '978-1905548927'),
        array('ID' => 4, 'booktitle' => 'The Crown Conspiracy', 'author' => 'Michael J. Sullivan',
                'isbn' => '978-0979621130'),
        array('ID' => 5, 'booktitle'     => 'Max Quick: The Pocket and the Pendant', 'author'    => 'Mark Jeffrey',
                'isbn' => '978-0061988929'),
        array('ID' => 6, 'booktitle' => 'Jack Wakes Up: A Novel', 'author' => 'Seth Harwood',
                'isbn' => '978-0307454355')
    );

    function get_columns(){
        $columns = array(
            'booktitle' => 'Title',
            'author'    => 'Author',
            'isbn'      => 'ISBN'
        );
        return $columns;
    }

    function prepare_items() {
        $columns = $this->get_columns();
        $hidden = $this->get_hidden_columns();
        $sortable = $this->get_sortable_columns();
        $this->_column_headers = array($columns, $hidden, $sortable);
        $this->items = $this->example_data;
    }

    function column_default( $item, $column_name ) {
        switch( $column_name ) {
            case 'booktitle':
            case 'author':
            case 'isbn':
                return $item[ $column_name ];
            default:
                return print_r( $item, true ) ;   //Show the whole array for troubleshooting purposes
        }
    }

    public function get_hidden_columns()
    {
        return array();
    }

    public function get_sortable_columns()
    {
        return array('title' => array('title', false));
    }
}

But I can't see anything.

  • 1
    column_default() needs to echo the output for the column. You're only returning values. – Jacob Peattie May 22 at 10:20
  • Thanks @JacobPeattie. How can I do that ? – abu abu May 22 at 10:24
  • 2
    Did you just copy this code from someone else? It's impossible to write this much code without knowing the difference between echoing and returning. If you're going to be messing around with code like this I suggest you stop everything and take some entry level PHP lessons online first. You're going to need them if you want to develop effectively with WordPress. But really, in this case, you literally just need to change return to echo. – Jacob Peattie May 22 at 10:27
  • Thanks @JacobPeattie. I know PHP basics but I am new in WordPress WP_List_Table. I tried with below code function column_default( $item, $column_name ) { switch( $column_name ) { case 'booktitle': case 'author': case 'isbn': echo $item[ $column_name ]; default: print_r( $item, true ) ; } }. But result is same like before. – abu abu May 22 at 10:56
  • 1
    Could you please share the class initialization code? I mean where you wrote $table = new myTable(); It'd be helpful if you could share that as well. – obiPlabon May 22 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.