php - Pagination in plugin's backend

admin2025-01-07  6

I am displaying a list of posts in the plugin backend but I can't seem to get the pagination to work. I used the standard approach of getting the "paged" variable and then passing it to get_posts()'s arguments, but whenever I visit my custom backend page or subpage and add the &page=2 variable I get that permissions error about not having enough of them to visit that page.

Is there a special way to apply pagination in the backend?

I am displaying a list of posts in the plugin backend but I can't seem to get the pagination to work. I used the standard approach of getting the "paged" variable and then passing it to get_posts()'s arguments, but whenever I visit my custom backend page or subpage and add the &page=2 variable I get that permissions error about not having enough of them to visit that page.

Is there a special way to apply pagination in the backend?

Share Improve this question asked Nov 29, 2015 at 23:30 NakedCatNakedCat 1891 gold badge2 silver badges9 bronze badges 2
  • What context are you doing this in? Is it on a post edit page? Options page? Generic admin page? – Tom J Nowell Commented Nov 29, 2015 at 23:46
  • @TomJNowell Custom submenu page added with add_submenu_page whose parent is a custom page added with add_menu_page. – NakedCat Commented Nov 29, 2015 at 23:48
Add a comment  | 

1 Answer 1

Reset to default 0

In the admin back-end of a plugin, you can't use the query_vars hook and the get_query_var built in function since the page itself is a query parameter. To get around this, I simply add a new query parameter to the end of your plugin page link called, '&pagenum=#" and I pull it out manually using:

$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;

There might be a better way of doing this, but I haven't found one that requires less effort.

Here is what the plugin url might look like:

admin.php?page=[plugin-dir]%2F[plugin-slug].php&pagenum=1
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736257242a423.html

最新回复(0)