最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - How do I create links at the top of WP_List_table?

programmeradmin7浏览0评论

I am trying to create links like the ones in the post tab (see image below).

I created my WP_List_table but I am not sure how to create those links or how to get them to show a different db query.

I know about the extra_tablenav function in the class but this adds things after the bulk action area, so this doesn't seem to do what I want.

I am trying to create links like the ones in the post tab (see image below).

I created my WP_List_table but I am not sure how to create those links or how to get them to show a different db query.

I know about the extra_tablenav function in the class but this adds things after the bulk action area, so this doesn't seem to do what I want.

Share Improve this question edited Apr 29, 2019 at 11:43 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Jun 28, 2012 at 22:32 Kirill FuchsKirill Fuchs 6186 silver badges15 bronze badges 3
  • possible duplicate of How to add a quicklink to the Posts Admin Published|Scheduled|Trash menu – Stephen Harris Commented Jun 29, 2012 at 12:22
  • Please see my solution to your question in this post. Close voting this a duplicate of that question. – Stephen Harris Commented Jun 29, 2012 at 12:22
  • Ignore that - this is a custom instance of WP_List_Table. Will post solution below. – Stephen Harris Commented Jun 29, 2012 at 12:24
Add a comment  | 

3 Answers 3

Reset to default 12

As mentioned in the comments, if this is an existing table you wish to add/remove links from, see this answer.

If this a custom subclass of WP_List_Table, e.g.:

class Wpse56883_WP_List_Table extends WP_List_Table{

 //Class methods here

}

Then you add these links by overriding the get_views() method. This should return an array: array ( id => link ). This should link back to the page, attaching some value to a query variable (which I'll call customvar).

When constructing the links, check for the current value for customvar and conditionally add the class current to highlight in bold the current view.

So (inside) your class.

function get_views(){
   $views = array();
   $current = ( !empty($_REQUEST['customvar']) ? $_REQUEST['customvar'] : 'all');

   //All link
   $class = ($current == 'all' ? ' class="current"' :'');
   $all_url = remove_query_arg('customvar');
   $views['all'] = "<a href='{$all_url }' {$class} >All</a>";

   //Foo link
   $foo_url = add_query_arg('customvar','foo');
   $class = ($current == 'foo' ? ' class="current"' :'');
   $views['foo'] = "<a href='{$foo_url}' {$class} >Foo</a>";

   //Bar link
   $bar_url = add_query_arg('customvar','bar');
   $class = ($current == 'bar' ? ' class="current"' :'');
   $views['bar'] = "<a href='{$bar_url}' {$class} >Bar</a>";

   return $views;
}

Then in your prepare_items method you can retrieve the customvar method and alter your query according to it's value.

 function prepare_items(){
     //Retrieve $customvar for use in query to get items.
     $customvar = ( isset($_REQUEST['customvar']) ? $_REQUEST['customvar'] : 'all');
 }

Note: The links can be used to perform actions. I would store the 'action' value in the query variable action (remember to use nonces!). Then hook onto load-{$hook} (see Codex), check permissions and nonces, and then perform the action.

If you are going to include 'action links', make sure you use nonces - and you should only display the link for users with the necessary capabilities.

You have to call views(); when rendering the page content.

Put <?php $wp_list_table->views(); ?> after page title and before the form.

See: \wp-admin\edit.php to see when is called.

For this problem i have created a custom style , you can follow

//Add a filter hook first 

add_filter('views_edit-your_custom_post_name',array($this,'prefix_post_status_links'));

 function prefix_post_status_links($views) {
         ?>
        <div class="gwf-tab-header">
            <ul class="nav nav-pills">
                <?php
                $views = array(
                 'all' => '
                <li class="nav-item">
                    <a class="nav-link active p-0" href="edit.php?post_type=your_custom_post_name">
                        <i class="fas fa-circle"></i>
                        All Entries
                    </a>
                </li>',
                'active' => '<li class="nav-item">
                    <a class="nav-link p-0 text-success" href="edit.php?post_status=active&post_type=your_custom_post_name">
                        <i class="far fa-circle"></i>
                        Active
                    </a>
                </li>',
                'inactive' => '<li class="nav-item">
                    <a class="nav-link p-0 text-warning" href="edit.php?post_status=inactive&post_type=your_custom_post_name">
                        <i class="far fa-circle"></i>
                        Inactive
                    </a>
                </li>',
                'trash' => '<li class="nav-item">
                    <a class="nav-link p-0 text-danger" href="edit.php?post_status=trash&post_type=your_custom_post_name">
                        <i class="far fa-circle"></i>
                        Trash
                    </a>
                </li>'
                );
                 return $views;
                ?>
            </ul>
        </div>
        <?php
    }
发布评论

评论列表(0)

  1. 暂无评论