简体   繁体   English

移动和桌面上的 Wordpress 分页不同

[英]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.

相关问题 WordPress:移动版和桌面版的不同标题图片 - WordPress: Different Header Images for Mobile + Desktop 移动和桌面使用不同的Wordpress Gallery缩略图大小吗? - Different Wordpress Gallery Thumbnail sizes for Mobile and Desktop? Wordpress分页按钮可在桌面上使用,但不能在移动或小屏幕上使用 - Wordpress Pagination Button working in desktop but not working in mobile or small screen 根据用户是移动用户还是桌面用户显示不同的标题图像? (WordPress,“二十七岁”) - Display a different header image depending on if the user is mobile or desktop? (Wordpress, “TwentySeventeen”) wordpress导航在桌面上显示手机 - wordpress nav displaying mobile on desktop 从移动或台式机Wordpress中检测用户 - Detect user from mobile or desktop wordpress 台式机上的Wordpress网站“服务不可用”,但在移动设备上可以使用 - Wordpress site “Service unavailable” on Desktop but working on mobile 在移动设备中完全展开下拉菜单,但在Wordpress上不完全展开桌面 - Full expand the dropdown menu in mobile, but not in desktop on Wordpress Wordpress 页面上的视频在桌面上加载但在移动设备上不加载 - Videos on Wordpress page loading on desktop but not on mobile Wordpress-在移动设备上使用桌面CSS - Wordpress - Using desktop CSS for mobile devices
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM