简体   繁体   English

根据一天中的时间更改 wordpress 登录页面图像

[英]Changing the wordpress login page image depending on the time of day

I've searched the internet for information on how to make the image of the login page to the admin panel in wordpress change depending on the time, but nothing works in my functions.php file.我在互联网上搜索了有关如何根据时间更改 wordpress 中管理面板的登录页面图像的信息,但在我的 functions.php 文件中没有任何效果。 I am showing you the code I used.我正在向您展示我使用的代码。 My guess is that I have done the wrong combination of java script and css in the php file.我的猜测是我在php文件中做了错误的java脚本和css组合。 I ask you guys how I could make the code to make it work.我问你们我如何制作代码以使其工作。

Additionally, I am wondering how to make the page use the local time of the person who is browsing the page, so that, for example, when someone is in New York, he can see the day background when it is daytime there, and someone from Moscow can have the day background and the night background in his own time.另外,我想知道如何使页面使用浏览页面的人的当地时间,例如,当有人在纽约时,他可以在那里看到白天的背景,而有人来自莫斯科的可以有他自己时间的白天背景和夜晚背景。

When everything works, I will of course apply it to the main page.当一切正常时,我当然会将其应用到主页。

Thank you for any help ;)感谢您的任何帮助 ;)

    <script type="text/JavaScript">
    var date = new Date();
    var hours = date.getHours();

    var sunset_img= "/images/2.png";

    var night_img = "/images/1.png";


    if((hours > 18)&&(hours < 6)){
    document.getElementById('feature_img').setAttribute('src', night_img);
    }
    else{
    document.getElementById('feature_img').setAttribute('src', sunset_img);
    }
    </script>
    <?php

    function my_login_logo() { ?>
    <style type="text/css">
        body.login
        {
        background-image: url(<?php echo get_stylesheet_directory_uri(); ?>feature_img);
        background-repeat: no-repeat;
        background-attachment: fixed;
        background-position: center;
        background-size:cover;
        }
}
    
<?php }
add_action( 'login_enqueue_scripts', 'my_login_logo' );

You can skip the image definition in the script and instead set a body class.您可以跳过脚本中的图像定义,而是设置主体类。 From there you can set an image in the CSS that corresponds to the class.从那里您可以在 CSS 中设置与类对应的图像。 JavaScript is run in the browser so inherently it will be local time for the user. JavaScript 在浏览器中运行,因此本质上它是用户的本地时间。

Using similar formatting you can do something like this, but using login_head since you are not enqueuing scripts:使用类似的格式你可以做这样的事情,但使用login_head因为你没有排队脚本:

<?php    
function my_login_logo() {
?>
    <script type="text/JavaScript">
        var date = new Date();
        var hours = date.getHours();
    
        if ((hours >= 18) || (hours <= 6)) {
            document.body.classList.add("background-image-night");
        }
        else {
            document.body.classList.add("background-image-sunset");
        }
    </script>
    <style type="text/css">
        body.login {
            background-repeat: no-repeat;
            background-attachment: fixed;
            background-position: center;
            background-size: cover;
        }

        body.login.background-image-night {
            background-image: url('<?php echo get_theme_file_uri('/images/night.jpg'); ?>');
        }

        body.login.background-image-sunset {
            background-image: url('<?php echo get_theme_file_uri('/images/sunset.jpg'); ?>');
        }
    </style>
<?php
}
add_action( 'login_head', 'my_login_logo' );

As I commented on your question you should prob put the code above in discrete files and enqueue them instead of outputting raw script and style tags.当我对您的问题发表评论时,您应该将上面的代码放在离散文件中并将它们排入队列,而不是输出原始scriptstyle标签。 Like so...像这样...

<?php
add_action( 'login_enqueue_scripts', function () {
    wp_enqueue_style( 'login-styles', get_theme_file_uri('styles/login-styles.css'), [], false );
    wp_enqueue_script( 'login-scripts', get_theme_file_uri('scripts/login-scripts.js'), [], false, true );
} );
?>

The above uses an anonymous function because we are only doing this once anyway and because we can in later versions of PHP.上面使用了一个匿名函数,因为无论如何我们只做一次,因为我们可以在以后的 PHP 版本中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM