Annonce an upcoming event using a simple post
You may need to publish some events on yours web pages to invite your visitors to a workshop, a concert, a meeting or an online presentation. There are a number of good WordPress plugins to manage events as the powerful Event manager available in a free version loaded of functionalities or a PRO version if you need online paiements, tickets, QR codes support and Woocommerce integration.
But if you want just annonce an upcoming event a plugin may be considered overkill and a simple post could be the solution.
In fact the post has everything that an event requires: a title, an image, a category, a date. A date? hum, no really!
Actually the post dates are intended as publishing dates consequently there is an internal WordPress rule that hide all post having a date greater than today. Not very convenient to annonce an event the same day and hoping that people will participate.
The following code must be inserted in the function.php contained in your child theme (**).
function futurePosts() {
$posts_per_page = 3;
$args = array(
'post_status' => 'publish,future',
'orderby' => 'publish_date',
'order' => 'DESC',
'posts_per_page' => $posts_per_page,
'date_query' => array(
array(
'after' => date('Y-m-d'),
'inclusive' => true,
)
)
);
$query = new WP_Query($args);
$modified_posts_query = new WP_Query($args);
$out = 'No future events.';
if($modified_posts_query->have_posts()) {
// if future posts exist replace $out content
$out = "";
while($modified_posts_query->have_posts()) {
$modified_posts_query->the_post();
$out.= '< a href="'.get_permalink($modified_posts_query->post->ID).'">';
$out.= get_the_title($modified_posts_query->post->ID);
$out.= '</a>';
$_format = ! empty( $format ) ? $format : get_option( 'date_format' );
$out.= get_post_time( $_format, false, $post, true );
}
}
return $out;
wp_reset_postdata();
}
add_shortcode('future_posts', 'futurePosts');
(**) How to Create a Child Theme
A child theme is a theme that inherits the functionality and styling of another theme, called the parent theme. Child themes are the recommended way of modifying an existing theme.
- Create child theme directory (common approach to name child theme as the parent theme plus -child added on the end.)
- Create style.css
- Create functions.php
For more detailed instructions on child themes you can visit the following links:
https://developer.wordpress.org/themes/advanced-topics/child-themes/
https://blog.hubspot.com/website/wordpress-create-child-theme