paulryken
Forum Replies Created
Viewing 1 replies (of 1 total)
-
Forum: Plugins
In reply to: [Dynamic Month & Year into Posts] Year change only after annual event heldThank you for this. You have prompted me to review the options. I also use ACF by WPEngine so created a custom field and added the following code to my functions.php.
function update_event_year_in_title($title) {
if (is_singular('post')) { // Replace 'post' with your custom post type if needed
$event_date = get_field('event_date');
if ($event_date) {
// Convert the event date to a timestamp using the correct format
$event_timestamp = DateTime::createFromFormat('d/m/Y', $event_date)->getTimestamp();
$current_timestamp = current_time('timestamp');
// Get the event year from the event date
$event_year = date('Y', $event_timestamp);
// Get the current year
$current_year = date('Y', $current_timestamp);
// Construct the event date for the current year
$event_day_month = date('m-d', $event_timestamp);
$current_year_event_date = strtotime($current_year . '-' . $event_day_month);
// Determine the correct year to display
if ($current_timestamp >= $current_year_event_date) {
// If current date is past or equal to the event date this year, use the next year
$event_year = $current_year + 1;
} else {
// Otherwise, use the current year
$event_year = $current_year;
}
// Replace [nyear] with the calculated event year
$title = str_replace('[nyear]', $event_year, $title);
}
}
return $title;
}
add_filter('the_title', 'update_event_year_in_title');
add_filter('pre_get_document_title', 'update_event_year_in_title'); // For meta titlesIt hasn’t quite worked yet, but could this be an a good alternative?
Viewing 1 replies (of 1 total)