【问题标题】:Change php args array depending on the window width根据窗口宽度更改 php args 数组
【发布时间】:2020-01-06 00:36:10
【问题描述】:

我正在使用 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 或如何以另一种类似的方式进行

【问题讨论】:

    标签: javascript php innerhtml args


    【解决方案1】:

    您可以通过ajax 实现此目的。这是一个例子:

    index.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>
    

    test.php

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

    【讨论】:

      猜你喜欢
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-30
      • 2016-07-14
      • 2012-09-28
      • 2012-04-21
      • 2012-06-29
      相关资源
      最近更新 更多