WordPress.org

Ready to get started?Download WordPress

Forums

Display Title of Post Depending on Time of Day (20 posts)

  1. SollyNZ
    Member
    Posted 4 years ago #

    Hi There,

    I'm currently working on a WordPress website for a radio station.

    The lineup for each day is broken up into daily categories as children ("monday", "tuesday" etc etc) of "lineup"

    Each post with in the category has two custom fields, a Start time "timestart" and an end time "timeend" all the posts have been entered using 24 Hour time. Example below

    timestart: 07:00
    timeend: 09:00

    etc etc

    Now what i want to do is have a global section in the header of my website that displays the current show which is on air depending on the time of the day.

    Does anybody have any ideas on how i can achieve this.

    Any help would be awesome

    Kind Regards
    - Solly

  2. SollyNZ
    Member
    Posted 4 years ago #

    I found this thread - http://wordpress.org/support/topic/249486?replies=16

    and tried this below

    <?php
    $thisday = date('l');
    if(isset($thisday)) {
    	switch($thisday) {
    		case 'Monday':
    			$post_to_query = 'category_name=monday';
    		break;
    		case 'Tuesday':
    			$post_to_query = 'category_name=tuesday';
    		break;
    		case 'Wednesday':
    			$post_to_query = 'category_name=wednesday';
    		break;
    		case 'Thursday':
    			$post_to_query = 'category_name=thursday';
    		break;
    		case 'Friday':
    			$post_to_query = 'category_name=friday';
    		break;
    		case 'Saturday':
    			$post_to_query = 'category_name=saturday';
    		break;
    		case 'Sunday':
    			$post_to_query = 'category_name=sunday';
    		break;
    		default:
    			// If the day is set but doesn't match the cases above
    			echo 'Something went wrong, day is not valid';
    			$errorday = strip_tags($thisday);
    			echo '<pre>'.$errorday.'</pre>';
    			$post_to_query = '';
    		break;
    	}
    } else {
    	// If the day is not set, which it should be.
    	echo 'Something went wrong, day is not valid (2) ';
    	$errorday = strip_tags($thisday);
    	echo '<pre>'.$errorday.'</pre>';
    	$post_to_query = '';
    }
    
    // Create a new loop query
    $newquery = new WP_Query($post_to_query);
    
    // If there are results
    if($newquery->have_posts()) {
    
    	// While there are results (the loop)
    	while($newquery->have_posts()) : $newquery-> the_post(); ?>
    
    		<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    
    	<?php
    	// End while loop
    	endwhile;
    
    	// Reset query after the loop
    	wp_reset_query();
    }
    else {
    	echo 'Query for '.$post_to_query.' failed to find a valid post.';
    }
    ?>

    Now this displays all the titles of each show per day, Any ideas how i can make it display the single title depending on the start time using the custom field "timestart"

  3. doc4
    Member
    Posted 4 years ago #

    SollyNZ,

    Try something like the following where you are setting an expiration and beginning time:

    <?php if( get_post_meta($post->ID, 'custom-field-name', true) ) { ?>
    <?php $exp_date = get('expired');
          $todays_date = date("Y m d");
          $today = strtotime($todays_date);
          $expiration_date = strtotime($exp_date);
             if ($expiration_date < $today) {
             echo "";
             } else {
             echo "Your Content";
             echo get('custom-field-name'); } ?>
    <?php } ?>
  4. SollyNZ
    Member
    Posted 4 years ago #

    Hey Doc4,

    Nothing seems to be displaying at all when i try that ?

    Swapped out the custom-field-name with timestart

    Any ideas ?

  5. SollyNZ
    Member
    Posted 4 years ago #

    Any Body else got any ideas ?

  6. MichaelH
    Member
    Posted 4 years ago #

    <?php
    $thisday = date('l');
    $thishour = date('H:i');
    $cat_id = get_cat_ID(strtolower($thisday));
    $start_key = 'timestart';
    $end_key = 'timeend';
    
    if ($cat_id) {
      $args=array(
        'category__in' => array($cat_id),
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post();
          $timestart = get_post_meta($my_query->post->ID, $start_key, true);
          $timeend = get_post_meta($my_query->post->ID, $end_key, true);
          if ( $thishour>=$timestart  && $thishour<=$timeend ) { ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
          }
        endwhile;
      }
      wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>
  7. SollyNZ
    Member
    Posted 4 years ago #

    Hey Micahel,

    Thank you for your reply

    That is displaying the first show of today, rather than the one at the correct time of today ?

    Any ideas

    Kind Regards
    - Solly

  8. SollyNZ
    Member
    Posted 4 years ago #

    are the values of my custom fields fine ?

    example:

    First Show
    timestart: 00:00
    timeend: 06:00

    Second Show
    timestart: 06:00
    timeend: 08:00

    Third Show
    timestart: 08:00
    timeend: 10:00

    Forth Show
    timestart: 10:00
    timeend: 12:00

    Fifth Show
    timestart: 12:00
    timeend: 14:00

    etc etc back to

    Last Show
    timestart: 22:00
    timeend: 00:00

  9. SollyNZ
    Member
    Posted 4 years ago #

    Also just looking at this line here

    $cat_id = get_cat_ID(strtolower($thisday));

    Say for example i have a show which is on the same time every day, So it would be posted in cats monday,tuesday,wednesday,thursday,friday,saturday,sunday by default the slug for this will be

    url.com/lineup/monday/showname

    Is this going to cause problems ? as when i hover over the current shot which displays with the code you posted. The slug for it is monday.

    Is this going to be a problem ? or do i need to create the same show for each day/category ?

  10. SollyNZ
    Member
    Posted 4 years ago #

    Sorry for bump but really need to solve this ><

  11. MichaelH
    Member
    Posted 4 years ago #

    Okay fixed two problems

    <?php
    /**
    Get current day (e.g. Monday) with date_i18n function and convert to lowercase and get a category ID for 'monday' or 'tuesday' etc.
    Get current time in 24:00 format
    Get all posts in category 'monday' or 'tuesday' etc.
    Loop through posts and get timestart and timeend custom fields
      If timeend is less than timestart add 25 to timeend to handle case where timestart is 23:00 and timeend is 00:00
      If current time is between timestart and timeend, display post title
     */
    
    $thisday = date_i18n('l');
    $thishour = date_i18n('H:i');
    $cat_id = get_cat_ID(strtolower($thisday));
    $cat_id = 1;
    $start_key = 'timestart';
    $end_key = 'timeend';
    
    if ($cat_id) {
      $args=array(
        'category__in' => array($cat_id),
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'caller_get_posts'=> 1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post();
          $timestart = get_post_meta($my_query->post->ID, $start_key, true);
          $timeend = get_post_meta($my_query->post->ID, $end_key, true);
          if ( $timeend < $timestart ) {
            $timeend = substr($timeend,0,2)+25 . substr($timeend,2,3);
          }
          if ( $thishour>=$timestart  && $thishour<=$timeend ) { ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
          }
        endwhile;
      }
      wp_reset_query();  // Restore global post data stomped by the_post().
    }
    ?>

    A show being in mulitple categories should not be a problem but you will have to live with the permalink thing--of course you could use a different permalink structure.

    Small note: timeend should be something like 10:59 or 00:59 or 23:59

  12. SollyNZ
    Member
    Posted 4 years ago #

    Legend Michael,

    Thats been working fine thank you so much, How ever since daylight savings has come around it has gone back 12 hours and is now displaying 12 hours earlier instead.

    I have tried changing date/time settings in the wordpress admin panel but nothing seems to change there.

    I have even set up a page to display the server time and it is correct.

    I also echoed the $thisday & $thishour and its displaying the wrong time/day

    Any ideas how i can resolve this ?

  13. SollyNZ
    Member
    Posted 4 years ago #

    Also i was just wondering if you knew how i could fix two other issues im having.

    I have a page called lineup.php which consists of a 7 scrolling panels (one for each day) below is my code for Monday, and each day is repeated this but obviously for each days category. There is also a Monday to sunday navigation at the top of the page where users can click on a day and it will take them to that panel.

    <div class="panel" id="monday">
                   <div class="postCopy" style="font-size:13px; line-height:1.8em;">
                   <h3 class="day">Monday</h3>
                   <ul>
                <?php $my_query = new WP_Query('category_name=monday&showposts=15&order=ASC');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
    
               <?php
    				$timestart = get_post_meta($post->ID, 'timestart', true);
    				$timeend = get_post_meta($post->ID, 'timeend', true);
    				$genre = get_post_meta($post->ID, 'genre', true);
    				$permalink = get_permalink();
    		?>
    				<li><p class="lineupTime"><?php echo $timestart; ?> - <?php echo $timeend; ?></p> <span class="showName"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a> -  (<?php echo $genre; ?>)</span></li>
    				<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
    
              <?php endwhile; ?> 
    
      <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    
      <?php endwhile; endif; ?>
                   <?php echo $category->category_description; ?>
                   </ul>
                   </div>
    
                     </div>

    My first question is:

    The $timeend custom field which i've had to set to 05:59,08:59 etc etc so it rolls over smoothley for each show looks a bit messy when echoed on the page. I was wondering if its possible to echo the timeend custom field but somehow make it display so instead of it being 05:59, it would be 06:00 - With out going into the custom fields and changing them to that?

    My Second question is:

    With each of those panels displaying, How can i make it so that when someone clicks on the lineup page the first panel which is displayed is the current day, rather than it always being Monday.

    Any help would be greatly appreciated.

  14. SollyNZ
    Member
    Posted 4 years ago #

    Actually wtf, weird my header.php file somehow restored an older version where i was using that first piece of code you posted Michael. Now that new code doesn't want to work at all ><

  15. SollyNZ
    Member
    Posted 4 years ago #

    Sorry for the multiple posts just using this as reference for my self

    Okay so i've gone back to that original code you posted Michael and that's working, How ever I've still got that issue of it displaying 12 hours behind.

    I tried adding this line of code from the second piece of code you posted

    if ( $timeend < $timestart ) {
            $timeend = substr($timeend,0,2)+25 . substr($timeend,2,3);
          }
          if ( $thishour>=$timestart  && $thishour<=$timeend ) { ?>

    How ever that doesn't change anything.

    The second lot of code you posted is not working at all for me now which is strange because it was.

    Sorry I dont understand much php so im a little confused here.

  16. SollyNZ
    Member
    Posted 4 years ago #

    Solved the issue,

    Changed the date in the first two lines to

    <?php
    $thisday = date_i18n('l');
    $thishour = date_i18n('H:i');
  17. SollyNZ
    Member
    Posted 4 years ago #

    Any body got any ideas for this post ?

    I have a page called lineup.php which consists of a 7 scrolling panels (one for each day) below is my code for Monday, and each day is repeated this but obviously for each days category. There is also a Monday to sunday navigation at the top of the page where users can click on a day and it will take them to that panel.

    <div class="panel" id="monday">
    <div class="postCopy" style="font-size:13px; line-height:1.8em;">
    <h3 class="day">Monday</h3>

      <?php $my_query = new WP_Query('category_name=monday&showposts=15&order=ASC');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>

      <?php
      $timestart = get_post_meta($post->ID, 'timestart', true);
      $timeend = get_post_meta($post->ID, 'timeend', true);
      $genre = get_post_meta($post->ID, 'genre', true);
      $permalink = get_permalink();
      ?>

    • <p class="lineupTime"><?php echo $timestart; ?> - <?php echo $timeend; ?></p> <span class="showName">" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?> - (<?php echo $genre; ?>)</span>
    • <?php wp_link_pages(array('before' => '<p>Pages: ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

      <?php endwhile; ?>

      <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>

      <?php endwhile; endif; ?>
      <?php echo $category->category_description; ?>

    </div>

    </div>

    My first question is:

    The $timeend custom field which i've had to set to 05:59,08:59 etc etc so it rolls over smoothley for each show looks a bit messy when echoed on the page. I was wondering if its possible to echo the timeend custom field but somehow make it display so instead of it being 05:59, it would be 06:00 - With out going into the custom fields and changing them to that?

    My Second question is:

    With each of those panels displaying, How can i make it so that when someone clicks on the lineup page the first panel which is displayed is the current day, rather than it always being Monday.

    Any help would be greatly appreciated.

  18. SollyNZ
    Member
    Posted 4 years ago #

    bump

  19. SollyNZ
    Member
    Posted 4 years ago #

    any ideas ;o sorry for bumping

  20. Darrell Schauss
    Member
    Posted 4 years ago #

    I know its related but is this a different question, issue? You fixed the original question already right?

    In that case I would make a new post and don't forget to use the CODE button =]. And reference this post in the new one if you need to. http://wordpress.org/support/topic/379105

Topic Closed

This topic has been closed to new replies.

About this Topic