admin管理员组文章数量:1122846
How do I determine if I'm on the very first page of pagination? I'm using WP_Pagenavi. I want to run a function only on the first page of the pagination. I checked the query_var 'paged', it's set to 0 on this page, and then 2, 3 and so on in the later pages (1 is missing!)... Anyone knows a clean solution?
Thanks.
How do I determine if I'm on the very first page of pagination? I'm using WP_Pagenavi. I want to run a function only on the first page of the pagination. I checked the query_var 'paged', it's set to 0 on this page, and then 2, 3 and so on in the later pages (1 is missing!)... Anyone knows a clean solution?
Thanks.
Share Improve this question asked Oct 14, 2011 at 5:58 Rutwick GangurdeRutwick Gangurde 8,6045 gold badges42 silver badges55 bronze badges3 Answers
Reset to default 31// get current page we are on. If not set we can assume we are on page 1.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// are we on page one?
if(1 == $paged) {
//true
}
if you only want to know that you're on the first page of a paginated page try is_paged()
:
if ( !is_paged() ) {
// first page of pagination
}
I was looking for a simple way to determine whether or not to use the posts_nav_link()
function and all solutions I found online were either too complex or unreliable. For example, many people suggested using the $paged
global variable, but I found that this variable returned the same value for the first page, even when the first page was the only page!
So, I dug into the wp-includes/link-template.php
file, and found that the posts_nav_link()
function simply outputs the return value of another function:
/**
* Display post pages link navigation for previous and next pages.
*
* @since 0.71
*
* @param string $sep Optional. Separator for posts navigation links.
* @param string $prelabel Optional. Label for previous pages.
* @param string $nxtlabel Optional Label for next pages.
*/
function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
$args = array_filter( compact('sep', 'prelabel', 'nxtlabel') );
echo get_posts_nav_link($args);
}
Using this knowledge, we can create a simple and effective way to determine whether or not we need to add links to navigate between pages:
$posts_nav = get_posts_nav_link();
if(empty($posts_nav)) {
// do not use posts_nav_link()
} else {
// use posts_nav_link()
}
Originally posted on my blog here.
本文标签: pagedHow to determine if I39m on the first page of pagination
版权声明:本文标题:paged - How to determine if I'm on the first page of pagination? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303650a1931958.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论