users - how do i get a greeting for logged in uses by the time of day

admin2025-01-07  8

hi i am looking to combing to pieces of code into 1 both work but not together keep no matter what i try it keeps crashing the site

<?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 ";
   } ?>

and

<?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(); } ?>

other user parameters would be great to swap out

hi i am looking to combing to pieces of code into 1 both work but not together keep no matter what i try it keeps crashing the site

<?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 ";
   } ?>

and

<?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(); } ?>

other user parameters would be great to swap out

Share Improve this question asked Dec 7, 2019 at 22:42 dsgdsg 212 bronze badges 1
  • Welcome to WordPress Development. I hope you find the answer(s) you are looking for. Our site is different from most - if you have not done so yet, consider checking out the tour and help center to find out how things work. – Matthew Brown aka Lord Matt Commented Dec 8, 2019 at 12:22
Add a comment  | 

2 Answers 2

Reset to default 0

I can see two very obvious flaws that might be holding you back.

$t=date("H"); gives you a formatted date as a string while if ($t<"12") and else if ($t<"18") are trying to do maths on words.

Consider $t=(int)date("H"); (force the answer to be a whole number) with if ($t<12) and else if ($t<18) which at least does maths with numbers.

This is the code you're looking for. WordPress has a current_time function that handles this, and then you can add some logic to get the user info.

Edit: this also could be used on page, but I prefer the use of it as a reusable function.

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();
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736264132a944.html

最新回复(0)