每周后台代码不起作用

时间:2016-09-12 作者:user102602

我有这个代码,我想在我的网站上实现。我已成功设置了一个儿童主题。假设此代码每周每天都更改背景。我对Wordpress不太熟悉,我想知道应该把它放在哪里,怎么放。有人能告诉我需要做什么吗?

代码如下所示。非常感谢你。

<小时>

function chgDailyImg()

{

var imagearray = new Array();

imagearray[0] = "sundaypic.jpg";

imagearray[1] = "mondaypic.jpg";

imagearray[2] = "tuesdaypic.jpg";

imagearray[3] = "wednesdaypic.jpg";

imagearray[4] = "thursdaypic.jpg";

imagearray[5] = "fridaypic.jpg";

imagearray[6] = "saturdaypic.jpg";



var d = new Date(); /*** create a date object for use ***/

var i = d.getDay(); /*** use the date object to get the day of the week - this will be a number from 0 to 6 - sunday=0, saturday=6 -it\'s the way counting works in javascript it starts at 0 like in the arrays ***/

document.getElementById("dailyImg").src = imagearray;

}/* CSS Document *//* CSS Document */

2 个回复
最合适的回答,由SO网友:Ethan O\'Sullivan 整理而成

在此场景中,要调用以执行函数的标记是wp_head().

看看您提供的代码,您已经有了想法,但我决定以不同的方式重写它。在您孩子的主题中functions.php 文件中,添加以下内容:

add_action( \'wp_head\', \'wpse_238911_weekly_background\' );
    function wpse_238911_weekly_background() {
    $day = date( "l" );

    switch( $day ) {
        case \'Monday\':
            $background_image = \'mon-img.jpg\';
            break;
        case \'Tuesday\':
            $background_image = \'tue-img.jpg\';
            break;
        case \'Wednesday\':
            $background_image = \'wed-img.jpg\';
            break;
        case \'Thursday\':
            $background_image = \'thu-img.jpg\';
            break;
        case \'Friday\':
            $background_image = \'fri-img.jpg\';
            break;
        case \'Saturday\':
            $background_image = \'sat-img.jpg\';
            break;
        case \'Sunday\':
        default:
            $background_image = \'sun-img.jpg\';
            break;
    }

    ?>
    <style type="text/css">
    body {
        background-image: url( \'http://web.site/img/<?php echo $background_image; ?>\' );
    }
    </style>
    <?php
}
只需关掉mon-img.jpg 更改为实际图像名称,并更改http://web.site/img/ 指向将存储一周中某一天图像的位置的路径。

SO网友:Dave Romsey

您发布的代码是JavaScript,应该放在子主题内的JavaScript文件中,例如:

/your-child-theme/js/background-changer.js

然后,您的子主题应该将JS文件从其functions.php 文件,如下所示:

function wpse238911_load_js() {
    wp_enqueue_script( \'wpse238911_load_js\', get_stylesheet_directory_uri() . \'/js/background-changer.js\', array(), false, false );
}
add_action( \'wp_enqueue_scripts\', \'wpse238911_load_js\' );
这确实是您的问题中唯一特定于WordPress的部分。

看起来你的JS也有一些问题。您正在分配整个imagearraysrc 的属性#dailyImg 要素看起来你应该使用imagearray[i]. 而且chgDailyImg() 从未执行。下面是一个可能有用的修复版本:

function chgDailyImg() {

    var imagearray = new Array();
    imagearray[0] = "sundaypic.jpg";
    imagearray[1] = "mondaypic.jpg";
    imagearray[2] = "tuesdaypic.jpg";
    imagearray[3] = "wednesdaypic.jpg";
    imagearray[4] = "thursdaypic.jpg";
    imagearray[5] = "fridaypic.jpg";
    imagearray[6] = "saturdaypic.jpg";

    var d = new Date(); /*** create a date object for use ***/
    var i = d.getDay(); /*** use the date object to get the day of the week - this will be a number from 0 to 6 - sunday=0, saturday=6 -it\'s the way counting works in javascript it starts at 0 like in the arrays ***/

    document.getElementById("dailyImg").src = imagearray[i];
}
chgDailyImg();
现在我们讨论的是香草JavaScript,这与本网站的主题无关。对于JavaScript问题,您可能会遇到堆栈溢出问题。