In the Genesis Facebook group a user asked:
I am using Magazine Pro with a number of custom fields created via ACF. I have set up the home page using the Genesis Featured Posts widget in a number of places. In one of the widget areas, I would like to remove the default post date info which displays as an overlay of the featured image and replace it with a date custom field. (The custom field is actually an event date that has no relationship to the post date). How do I do this?
There are two ways in which this can be done.
With method 1, you have to replace [post_date] with [event_date] where needed. Ex.: in the widgets.
Method 2 is automatic and works site-wide. Post date (wherever it appears) gets replaced with the custom field’s value.
In both the methods, we shall ensure that if there’s no custom date present, the standard post (published) date will be set to appear.
First, let’s create our custom date field.
Install and activate Advanced Custom Fields.
Add a new field group called say, Post Meta
having a field of type Date Picker
like so (XML export file):
Edit your Post(s) and set your Event Date using the date picker.
Method 1 – Using a custom shortcode
Let’s create a custom [event_date]
shortcode that checks for event_date
custom field and displays its value if present and if not, the standard post (published) date.
Add the following in child theme’s functions.php:
add_shortcode( 'event_date', 'custom_event_date_shortcode' );
/**
* Create `event_date` shortcode.
*
* @return string Value of a `event_date` custom field. If the custom field is empty, entry published date.
*/
function custom_event_date_shortcode() {
// get and store the value of event_date custom field.
$event_date = get_post_meta( get_the_ID(), 'event_date', true );
if ( $event_date ) {
// make date object.
$event_date = new DateTime( $event_date );
return sprintf( '<time %s>', genesis_attr( 'entry-time' ) ) . $atts['before'] . $atts['label'] . $event_date->format( get_option( 'date_format' ) ) . $atts['after'] . '</time>';
} else {
return do_shortcode( '[post_date]' );
}
}
Now edit your widget(s) and replace, for example,
[post_date] By [post_author_posts_link] [post_comments]
with
[[event_date]] By [post_author_posts_link] [post_comments]
Method 2 – Using the genesis_post_date_shortcode
filter hook
Add the following in child theme’s functions.php:
add_filter( 'genesis_post_date_shortcode', 'custom_post_date_shortcode', 10, 2 );
/**
* Produces the date of post publication.
* If the post's `event_date` custom field is present, its value will be shown. Otherwise, the post date.
*
* @param string $output Existing output for `post_date` shortcode.
* @param array|string $atts Shortcode attributes. Empty string if no attributes.
* @return string Modified output for `post_date` shortcode.
*/
function custom_post_date_shortcode( $output, $atts ) {
// get and store the value of event_date custom field.
$event_date = get_post_meta( get_the_ID(), 'event_date', true );
if ( $event_date ) {
// make date object.
$event_date = new DateTime( $event_date );
if ( 'relative' === $atts['format'] ) {
$display = genesis_human_time_diff( $event_date->format( 'U' ), current_time( 'timestamp' ), $atts['relative_depth'] );
$display .= ' ' . __( 'ago', 'genesis' );
} else {
$display = $event_date->format( $atts['format'] );
}
$output = sprintf( '<time %s>', genesis_attr( 'entry-time' ) ) . $atts['before'] . $atts['label'] . $display . $atts['after'] . '</time>';
}
return $output;
}
References:
advancedcustomfields.com/resources/date-picker/
genesis/lib/shortcodes/post.php
That’s a nice tutorial Sridhar. I think now a days in all themes this option should be there to replace the post date with updated date. Is this what you are trying to say here? Please correct me if I am wrong.