Hey! Long time lurker, first time posting.
I am working on a restaurant website that has daily food features. My goal is to be able to publish the week’s worth of features, and have the homepage display that given day’s post automatically.
Mon. – Ham
Tues. – Turkey
Wed. – Veggie
I have setup a custom field for ‘Feature Date’ using the Date Picker.
Here is what I have for the homepage:
<?php query_posts( 'post_type=feature&posts_per_page=1'); ?> <?php while (have_posts()) : the_post(); ?> <span class="medium-text">DAILY FEATURE FOR <?php the_time('l, F jS, Y') ?></span> <span class="small-text"><?php echo get_the_title(); ?></span> <span class="image-medium"><img src="<?=get_field('feature_image')?>" alt="Image"/></span> <?php endwhile; ?><?php wp_reset_query(); ?>
I really should probably have the ‘the_time’ be using the custom field. But I am unsure how to say ‘Find the post for 7/10/13 and display it’. And then the next day, find post for 7/11/13 and display it… and so on.
Many, many thanks in advance.
This task can be spit up into a few tasks:
1. Find today’s date. This can be done with the date function is PHP. Please research this first, but it will look something like this:
$today = date('Ymd');
2. Next step is to add the date field to the ‘feature’ post type. It seems like you have already done this. Make sure the save_format is ‘yymmdd’ This JS format will match the PHP format of Ymd
3. Lastly, just add in a meta query into the query_posts parameters. Please research this, but it will look something like this:
<?php query_posts( 'post_type=feature&posts_per_page=1&meta_key=date_field_name&meta_value=' . $today); ?>
Hope that helps
Thanks, Elliot. This totally worked. Much appreciated.
It seems that either my server is off, or my code – but I set a Feature for today, but the site doesn’t show it. If I change it to be the feature for tomorrow, it works. So it’s like the PHP date is off. Does this have something to do with my shared server?
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
© 2020 Advanced Custom Fields. Subscribe