在移动屏幕上更改每页显示的帖子数。

huangapple go评论52阅读模式
英文:

change the value of posts_per_page on mobile screen

问题

我有这段代码:

$args = array('post_type' => 'development', 'posts_per_page' => 1, 'order' => 'ASC');
$loop = new WP_Query($args);
while ($loop->have_posts()) : $loop->the_post();
?>
   // 这里是HTML代码
<?php
endwhile;
?>

我想在移动屏幕上将 posts_per_page 的值更改为 (-1) 以获取所有文章。

我尝试使用 wp_is_mobile,但我认为它在平板电脑上返回 true,而我不希望如此。 有什么帮助吗?

英文:

I have this code:

$args = array(&#39;post_type&#39; =&gt; &#39;development&#39;, &#39;posts_per_page&#39; =&gt; 1, &#39;order&#39; =&gt; &#39;ASC&#39;); 
$loop = new wp_query($args);
while ($loop-&gt;have_posts()) : $loop-&gt;the_post();

?&gt;
   //html code here
&lt;?php
endwhile;
?&gt;

and I want to change the value of posts_per_page to (-1) on mobile screen to get all posts.

I tried to use

wp_is_mobile

but I think it returns true when it is a tablet and I don't want that. Any help?

答案1

得分: 0

wp_is_mobile检查$_SERVER['HTTP_USER_AGENT'],无法区分手机和平板电脑。
解决这个问题有几个选项:
1)使用库https://github.com/serbanghita/mobile-detect

$detect = new \Detection\MobileDetect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');

2)通过JavaScript检查设备,通过Ajax接收帖子(可能会影响SEO):

if (window.matchMedia('(min-width:1024px)').matches) {
    // desktop
}
if (window.matchMedia('(max-width:1023px) and (min-width:768px)').matches) {
    // tablet
}
if (window.matchMedia('(max-width:767px)').matches) {
    // mobile
}

3)以不同的块中显示相同的帖子(似乎是最差的选择),例如:

<div class="hidden-mobile">
    <!-- 帖子 -->
</div>
<div class="visible-mobile">
    <!-- 帖子 -->
</div>
@media (max-width: 767px) {
  .hidden-mobile {
    display: none;
  }
}
@media (min-width: 768px) {
  .visible-mobile {
    display: none;
  }
}
英文:

wp_is_mobile checks $_SERVER[&#39;HTTP_USER_AGENT&#39;] and cannot distinguish between a phone and a tablet.
There are several options for solving the problem:

  1. Use the library https://github.com/serbanghita/mobile-detect
$detect = new \Detection\MobileDetect;
$deviceType = ($detect-&gt;isMobile() ? ($detect-&gt;isTablet() ? &#39;tablet&#39; : &#39;phone&#39;) : &#39;computer&#39;);
  1. Receive posts via Ajax (may affect SEO) by checking the device via javascript:
if(window.matchMedia(&#39;(min-width:1024px)&#39;).matches){
	// desktop
}
if(window.matchMedia(&#39;(max-width:1023px) and (min-width:768px)&#39;).matches){
	// tablet
}
if(window.matchMedia(&#39;(max-width:767px)&#39;).matches){
	// mobile
}
  1. Display the same posts in different blocks (seems the worst option), for example:
&lt;div class=&quot;hidden-mobile&quot;&gt;
	&lt;!-- posts --&gt;
&lt;/div&gt;
&lt;div class=&quot;visible-mobile&quot;&gt;
	&lt;!-- posts --&gt;
&lt;/div&gt;
@media (max-width: 767px) {
  .hidden-mobile {
    display: none;
  }
}
@media (min-width: 768px) {
  .visible-mobile {
    display: none;
  }
}

huangapple
  • 本文由 发表于 2023年3月15日 20:08:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75744487.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定