【问题标题】:refreshing a div content in 10 sec in wordpress website在 10 秒内刷新 wordpress 网站中的 div 内容
【发布时间】:2019-09-19 00:34:57
【问题描述】:

我正在使用 WordPress。 我需要每 10 秒刷新一次 div。 div 的内容是一个简码,其中包含来自数组的图像。

div 名称是 header_image,创建短代码的代码在 functions.php 中

所以添加了这段代码,但没有任何效果

 <script src="http://code.jquery.com/jquery-latest.js"></script>

    <script>
        $(document).ready(function(){
            setInterval(function() {
                $(".header_image").load("functions.php");
            }, 1000);
        });

    </script>

div的内容

<div class="header_image">
    <a href="<?php echo $data[0]; ?>">
<img  src="<?php echo $data[1]; ?>">
</a>
</div>

【问题讨论】:

  • 10 秒内你应该使用10000 而不是1000
  • load() 方法从服务器加载数据并将返回的数据放入选定的元素中。所以 ` ">` 这些内容应该出现在 php 中
  • @Deepak 。它在 php 中。图像显示在网站上。我在刷新部分有问题
  • 打开浏览器的控制台查看任何错误。请记住,您应该使用 admin-ajax.php 来执行 ajax 工作,而不是创建新文件。
  • @AnhTuan 我收到 functions.php 的 404 错误

标签: jquery wordpress require-once add-filter


【解决方案1】:

这解决了这个问题。在函数中添加了 JS,并使用 setinterval 每 10 秒更换一次图像

<script>
var $image_square = <?php echo json_encode($image_array_square); ?>;
var $link_square =<?php echo json_encode($link_array_square); ?>;
        var i = 0;
        var renew = setInterval(function(){
            if(links.length == i){
                i = 0;
            }
            else {
            document.getElementById("squareImage").src = $image_square[i]; 
            document.getElementById("squareLink").href = $link_square[i]; 
            i++;

        }
        },10000);
        </script>
//To display the first image in the website,taking the 2nd image from array/

<div class="square_image">
<a id="squareLink" href="<?php echo $link_array_square[2]; ?>" onclick="void window.open(this.href); return false;">
<img id="squareImage" src="<?php echo $image_array_square[2]; ?>" >
</a>
</div>
</div>

【讨论】:

    【解决方案2】:

    如果浏览器返回 404 Not found,你应该使用绝对路径而不是相对路径。

    例子:如果你的functions.php文件在/wp-content/themes/theme-name/functions.php,正确的代码应该是:

    $(".header_image").load("/wp-content/themes/theme-name/functions.php");
    

    【讨论】:

    • 我认为这是解决方案,但是在调用 php 文件时出现错误 Call to undefined function add_shortcode()
    • 然后将此行添加到您的functions.php文件的开头:require_once( ABSPATH . '/wp-includes/shortcodes.php' );
    • 不能使用 require_once( ABSPATH . '/wp-includes/shortcodes.php' );使用 ABSPATH 会显示错误。所以我添加了 require_once('the full path' );现在显示第一张图像,几秒钟后产生错误。调用未定义函数 add_filter()
    猜你喜欢
    • 2021-06-06
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    • 2016-02-21
    相关资源
    最近更新 更多