Wordpress Coding Help

by cyas22
5 replies
Is there any way possible that a feature page/post automatically changes everyday? I mean you already have a post/page for each day of the week and every day the featured post/page on your homepage will change to each specific post/page that you have for each day? I kinda tried searching for an "ifelse" function in php/css but I guess there is no such function/substitute that I know of. Can anyone help?
#coding #wordpress
  • Profile picture of the author dwoods
    Yes of course it's possible.
    *edit* WF stripped out my instances of ($)post
    PHP Code:
    <?php

     
    date('l'time());

    if( == 
    'Sunday'){
        
    //show sunday post
         
    get_post(POST_ID_HERE);
        
    }else if( == 
    'Monday'){
        
    //show monday post
         
    get_post(POST_ID_HERE);

    }else if( == 
    'Tuesday'){
        
    //show tuesday post
         
    get_post(POST_ID_HERE);

    }else if( == 
    'Wednesday'){
        
    //show wednesday post
         
    get_post(POST_ID_HERE);

    }else if( == 
    'Thursday'){
        
    //show thursday post
         
    get_post(POST_ID_HERE);

    }else if( == 
    'Friday'){
        
    //show friday post
         
    get_post(POST_ID_HERE);

    }else if( == 
    'Saturday'){
        
    //show saturday post
         
    get_post(POST_ID_HERE);
    }
    ?>
    {{ DiscussionBoard.errors[7043407].message }}
    • Profile picture of the author cyas22
      Originally Posted by dwoods View Post

      Yes of course it's possible.
      *edit* WF stripped out my instances of ($)post
      PHP Code:
      <?php

       
      date('l'time());

      if( == 
      'Sunday'){
          
      //show sunday post
           
      get_post(POST_ID_HERE);
          
      }else if( == 
      'Monday'){
          
      //show monday post
           
      get_post(POST_ID_HERE);

      }else if( == 
      'Tuesday'){
          
      //show tuesday post
           
      get_post(POST_ID_HERE);

      }else if( == 
      'Wednesday'){
          
      //show wednesday post
           
      get_post(POST_ID_HERE);

      }else if( == 
      'Thursday'){
          
      //show thursday post
           
      get_post(POST_ID_HERE);

      }else if( == 
      'Friday'){
          
      //show friday post
           
      get_post(POST_ID_HERE);

      }else if( == 
      'Saturday'){
          
      //show saturday post
           
      get_post(POST_ID_HERE);
      }
      ?>
      Thanks, I'm a bit sleepy to try this out though and will probably be out in a few. This was a big help.
      Signature

      For quality, affordable and reliable SEO services just email me. aacabanal@gmail.com

      {{ DiscussionBoard.errors[7043430].message }}
      • Profile picture of the author dwoods
        Originally Posted by cyas22 View Post

        Thanks, I'm a bit sleepy to try this out though and will probably be out in a few. This was a big help.
        Okay, great should do what you need it to. It's important to note that WF stripped out my variable names from the code ... I've attached a .txt file with the code that hasn't been goofed by the board.
        {{ DiscussionBoard.errors[7044457].message }}
  • Profile picture of the author klickgablow
    Here is an additional solution you can try as an alternative to a large if/elseif/else block;

    Code:
    //replace the post IDs for each day respectively
    $featured = array(
    
    		 'Sunday'    => 100
    		,'Monday'    => 200
    		,'Tuesday'   => 300
    		,'Wednesday' => 400
    		,'Thursday'  => 500
    		,'Friday'    => 600
    		,'Saturday'  => 700
    
    	);
    	
    $day      = the_date('l'); 
    $id       = $featured[$day];
    $the_post = new WP_Query( 'p=' . $id  );
    
    if ( have_posts() ) : while ( $the_post->have_posts() ) : $the_post->the_post();
    	
    	//you can edit this according to what you want to show
    	the_title()   . '<br/>';
    	the_date()    . '<br/>';
    	the_content() . '<br/>';
    	
    endwhile; endif;
    Explanation

    You need only change the post IDs within,

    Code:
    $featured = array(
    
    		 'Sunday'    => 100 //change me
    		,'Monday'    => 200 //change me
    		,'Tuesday'   => 300 //change me
    		,'Wednesday' => 400 //change me
    		,'Thursday'  => 500 //change me
    		,'Friday'    => 600 //change me
    		,'Saturday'  => 700 //change me
    
    	);
    The way this works is simple...

    Code:
    //gets the current day i.e. Sunday
    //and stores the day in the variable $day
    $day      = the_date('l');
    Code:
    //next we get the ID number that corresponds with the key 'Sunday' in the array
    //which in the example is ID of 100 and then we store that value in the $id variable
    $id       = $featured[$day];
    Code:
    //next we call WP_Query and pass in one paramter which is the $id variable
    //in this case its Post ID 100
    $the_post = new WP_Query( 'p=' . $id  );
    ...and as such our query goes on its merry way to fetch the post ID 100.

    On Monday, this will automatically fetch ID 200. Etc...
    {{ DiscussionBoard.errors[7049708].message }}
    • Profile picture of the author crivion
      Why don't you just use the wordpress built-in feature for scheduling a post publish date and time?

      Check attachment. Find the thing in wp-admin->right hand side before Publish button
      {{ DiscussionBoard.errors[7053312].message }}

Trending Topics