[英]Wordpress pagination different on mobile and desktop
Is it possible to use the default Wordpress pagination but have different numbers on mobile and desktop?是否可以使用默认的 Wordpress 分页,但在移动设备和桌面设备上有不同的数字? For example, I would like to show only 3 reviews per page on mobile and 5 on desktop.例如,我想在移动设备上每页仅显示 3 条评论,在桌面设备上显示 5 条评论。
The way to do this for all devices was described in this post:在这篇文章中描述了对所有设备执行此操作的方法:
Setting limited number of reviews on product page in WooCommerce 在 WooCommerce 中的产品页面上设置有限数量的评论
However, that does it for every device.但是,这适用于每个设备。
wp_is_mobile() is a method that test if the current user browser runs on any mobile device. wp_is_mobile()是一种测试当前用户浏览器是否在任何移动设备上运行的方法。
To change the number of reviews (or posts) per page, you should set the parameter posts_per_page
in the main query.要更改每页的评论(或帖子)数量,您应该在主查询中设置参数posts_per_page
。
You can do so by adding the following code into the functions.php
file:您可以通过将以下代码添加到functions.php
文件中来实现:
add_action( 'pre_get_posts', 'change_posts_per_page' );
function change_posts_per_page( $query ) {
if( !$query->is_main_query() || is_admin() ) return;
$posts_per_page = wp_is_mobile() ? 3 : 5;
$query->set( 'posts_per_page', $posts_per_page );
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.