我如何按一天中的时间获得已登录使用的问候语

时间:2019-12-07 作者:dsg

嗨,我期待着把一段段代码合并成一段,两个都可以,但不能放在一起,无论我尝试什么,它都会使网站崩溃

<?php
$t=date("H");
 if ($t<"12")
   {
   echo " Have a good morning!  $display_name ";
   }
 else if ($t<"18")
   {
   echo " Have a good afternoon!   $display_name ";
   }
else
{
   echo " Have a good evening!   $display_name ";
   } ?>
以及

<?php global $current_user; wp_get_current_user(); ?>
<?php if ( is_user_logged_in() ) { 
 echo \'Welcome Brother: \' . $current_user->user_login . "\\n";  } 
else { wp_loginout(); } ?>
其他用户参数最好换掉

2 个回复
SO网友:Matthew Brown aka Lord Matt

我可以看到两个非常明显的缺陷,这可能会阻碍你前进。

$t=date("H"); 以字符串形式为您提供格式化日期,而if ($t<"12")else if ($t<"18") 正在努力做单词数学。

考虑$t=(int)date("H"); (强制答案为整数)带if ($t<12)else if ($t<18) 至少数学和数字是一样的。

SO网友:John McCulley

这就是您要查找的代码。WordPress有一个current\\u time函数来处理这个问题,然后您可以添加一些逻辑来获取用户信息。

编辑:这也可以在页面上使用,但我更喜欢将其用作可重用的功能。

function heyBro(){
        global $current_user;
        wp_get_current_user();
        if (is_user_logged_in()) {
            $display_name = $current_user->user_login;
            $t = current_time(\'H\');
            $salutation = \'\';
            if ($t < \'12\') {
                $salutation = \'morning\';
            } else if ($t < \'18\') {
                $salutation = \'afternoon\';
            } else {
                $salutation = \'evening\';
            }
            return \'Have a good \' . $salutation . \', \' . $display_name . \'!\';
        }

        wp_loginout();
}

相关推荐

Users activity history

当我们更改代码上的一些问题时,wordpress web出现了一些问题,我想确定是哪些更改导致了web崩溃。我尝试了一些插件作为简单的历史记录,但问题是,您无法在安装插件之前获得所做的活动。有人知道即使我们没有安装插件,我如何才能获得网站的全部活动吗?提前谢谢。