简体   繁体   中英

Wordpress how can I know if the pagination is active?

Has Wordpress a function or something like that?I need a way to check if there are any page links(Older Entries | Newer Entries)to be displayed or not.

Best Regards,

If you look at the new_posts_link function, you'll see a $max_page and $paged vars.

If the $pages is higher to 1, there is a previous page link.
It it's smaller to $max_page, there is a next page link.

So you can do the following functions :

# Will return true if there is a next page
function has_next_page() {
    global $paged, $max_page;
    return $paged < $max_page;
}

# Will return true if there is a previous page
function has_previous_page() {
    global $paged;
    return $paged > 1;
}

# Will return true if there is more than one page (either before or after).
function has_pagination() {
    return has_next_page() or has_previous_page();
}

正如Marcus所说,函数has_next_page()不适用于较新的wp版本,因此您只需在if语句中使用get_previous_posts_link()get_next_posts_link()

虽然我和Lester Chan.right一样使用了pagenavi插件http://lesterchan.net/wordpress/readme/wp-pagenavi.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM