skip to Main Content

I’m trying to automatically display different content each day of the week, using PHP switch case conditional statements, Example: I want to automatically display a particular content on Monday, different content on Tuesday, different on Wednesday and the rest, So i’m using the below code to display it through wordpress function.php, the below code worked just as i wanted but the only one problem, is that when i use the shortcode on a WordPress page where pagebuilder is activated, the out put does not display on the actual page where i pasted the shortcode but in the header, So i’m trying to make it display normally like any other shortcode, Please any help will be appreciated, Thanks.

I have latest version of WordPress installed

function todays_content(){

$date = date('l');
switch ($date) {
  case 'Sunday': $content = do_shortcode('[sunday_content]'); break;
  case 'Monday': $content = do_shortcode('[monday_content]'); break;
  case 'Tuesday': $content = do_shortcode('[tuesday_content]'); break;
  case 'Wednesday': $content = do_shortcode('[wednesday_content]'); break;
  case 'Thursday': $content = do_shortcode('[thursday_content]'); break;
  case 'Friday': $content = do_shortcode('[friday_content]'); break;
  case 'Saturday': $content = do_shortcode('[saturday_content]'); break;
  }
  echo '<div class="daily_contant">'.$content.'</div>';
}
add_shortcode( 'show_today_content', 'todays_content' );

2

Answers


  1. Chosen as BEST ANSWER
    function todays_content(){
    
        $date = date('l');
        switch ($date) {
          case 'Sunday': $content = do_shortcode('[sunday_content]'); break;
          case 'Monday': $content = do_shortcode('[monday_content]'); break;
          case 'Tuesday': $content = do_shortcode('[tuesday_content]'); break;
          case 'Wednesday': $content = do_shortcode('[wednesday_content]'); break;
          case 'Thursday': $content = do_shortcode('[thursday_content]'); break;
          case 'Friday': $content = do_shortcode('[friday_content]'); break;
          case 'Saturday': $content = do_shortcode('[saturday_content]'); break;
          }
          return '<div class="daily_contant">'.$content.'</div>';
        }
        add_shortcode( 'show_today_content', 'todays_content' );
    

  2. It’s not necessary to use a switch in this case 🙂 . You should return and not echo it instead

    function todays_content(){   
        $today = date('l');
        return "Today is {$today}";
    }
    
    add_shortcode( 'show-today-content', 'todays_content' );
    

    Usage:

    [show-today-content]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search