根据窗口宽度更改 php 参数数组



我正在使用javascript来检测窗口宽度:

<script type="text/javascript">  
if ($(window).width() > 1200) {
document.getElementById("box").innerHTML="1140";}
else if ($(window).width() > 1050 && $(window).width() < 1199) {
document.getElementById("box").innerHTML="970";}
else if ($(window).width() > 820 && $(window).width() < 1049) {
document.getElementById("box").innerHTML="760";}
</script>

这是工作,但我需要根据显示器宽度更改 php 数组。

<?php $args = array(
'post_type'      => 'post',
'posts_per_page' => 6,
); ?>

因此,"每页帖子"应根据显示器宽度而变化

如何将每页的帖子参数放在 innerHTML 中,或者如何以另一种类似的方式制作它

您可以使用ajax来实现此目的。下面是一个示例:

索引.html

<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript"> 
function sendParams(arg) {
$.post('test.php', {params: arg}, function(data) { 
console.log(data);
});
} 
if ($(window).width() > 1200) {
sendParams("1140");
}
else if ($(window).width() > 1050 && $(window).width() < 1199) {
sendParams("970");
}
else if ($(window).width() > 820 && $(window).width() < 1049) {
sendParams("760");
}
</script>
</body>
</html>

测试.php

<?php
if(isset($_POST['params'])) {
$args = array(
'post_type'      => 'post',
'posts_per_page' => $_POST['params']
);
}
?>

最新更新