- I have a speakers category
- Each speaker is set up as a post
- The wordpress published date, is the date they are coming to speak (so most speakers posts are “scheduled”)
- There is a new speaker each week (every 7 days)
- On my speakers page I display the “featured speaker of the week” which contains all the post information
- Underneath the featured speaker, is a list of upcoming speakers (just the title, not the whole post)
What I’m trying to accomplish is to have the “Featured Speaker”, only show up for a week (including the day they are speaking), and once the day after they have spoken comes, it will show the next person speaking on the site for a week until they have spoken and so on and so forth.
I’m getting stumped on the logic. I was doing well until I hit:
if($postdatenum >= $todaynum + 6)
Which will not work when it’s a different month.
I know my code is convoluted and probably outdated, but if anyone can shed some light on the logic for calculating the 7 days thing, that would be awesome.
Here’s what I have so far:
<?php
query_posts(array(
'post_type' => 'post',
'category_name' => 'speakers',
'showposts' => 1,
'order' => DESC
) );
while (have_posts()) : the_post(); ?>
<div class="">
<?php
$today = date('m/d');
$postdate = the_date('m/d','','',FALSE);
//IF POST DATE AND TODAYS DATE NOT THE SAME
if($postdate != $today){
wp_reset_query();
query_posts(array(
'post_status' => 'future',
'showposts' => 1,
'order' => ASC
) );
while (have_posts()) : the_post();
$todaynum = date('d'); //10
$postdatenum = the_date('d','','',FALSE); //17
if($postdatenum >= $todaynum + 6){
?>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail(('medium'), array('class' => 'alignright')); ?>
<?php the_content(); ?>
<?php
}
endwhile;
wp_reset_query();
}else{
?>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail(('medium'), array('class' => 'alignright')); ?>
<?php the_content(); ?>
<?php
} endwhile;
?>
use strtotime and date
not sure what format you are using but look here for diff formats http://php.net/manual/en/function.date.php. You obviously cant use just the day because months and years are involved.
I wonder if this trick with the
date_query
will work for you as a single query (untested):The first post from this query should be the current speaker this week and the other post after that will be the upcoming speaker.
If you want to include more upcoming speakers, you can increase the
posts_per_page
and increase the number of days inbefore
parameter.So no need for more than a single query.
ps: Don’t use
query_posts()
, it’s not recommended, check for example this quote from the Codex:Example:
Let’s assume the lectures always starts at 12:00 pm each week.
Then we mark the speakers (A, B, C, D, E, … ) on the calendar:
Sorry for the mismatched +/- and the current day * in the above. It’s just an example how you can mark the calendar.
Then we can construct the following list from the above query: