Skip to content

Instantly share code, notes, and snippets.

@KZeni
Last active June 27, 2022 16:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KZeni/1c9d2204f553457a8b034696d4e472ec to your computer and use it in GitHub Desktop.
Save KZeni/1c9d2204f553457a8b034696d4e472ec to your computer and use it in GitHub Desktop.
Add start date from The Events Calendar to Display Posts plugin's output
<?php
/**
* Add custom fields to Display Posts Shortcode
* @author Bill Erickson
* @link http://wordpress.org/extend/plugins/display-posts-shortcode/
* @link http://www.billerickson.net/shortcode-to-display-posts/comment-page-1/#comment-4565
*
* @param $output string, the original markup for an individual post
* @param $atts array, all the attributes passed to the shortcode
* @param $image string, the image part of the output
* @param $title string, the title part of the output
* @param $date string, the date part of the output
* @param $excerpt string, the excerpt part of the output
* @return $output string, the modified markup for an individual post
*/
function display_posts_custom_fields($output, $atts, $image, $title, $date, $excerpt)
{
// Get our custom fields
global $post;
if (isset($atts['show_event_date']) && $atts['show_event_date'] == 'true') {
$event_date = tribe_get_start_date($post->ID, false, 'M j');
}
// If there's a value for the custom field, let's wrap them with <span>'s so you can control them with CSS
if (isset($event_date)) {
$event_date = '<span class="event-date">' . $event_date . '</span> ';
}
// Now let's rebuild the output.
$output = '<div>' . $image . $title . $date . $event_date . $author . $category_display_text . $excerpt . $content . '</div>';
// Finally we'll return the modified output
return $output;
}
add_filter('display_posts_shortcode_output', 'display_posts_custom_fields', 10, 6);
@KZeni
Copy link
Author

KZeni commented Jun 27, 2022

This specifically adds the start date from The Events Calendar to the output if show_event_date="true" in the [display-posts] shortcode's attributes.

Meanwhile, the setup/wrapper for this allows for any number of adjustments to be made to the output per https://gist.github.com/billerickson/1209601 (and/or other similar items using the display_posts_shortcode_output filter like those at https://displayposts.com/docs/the-output-filter/ [which can offer more recent examples.])

It just so happens the only thing being added here is the event date (definitely making sure the $output is then matching what you want as the end result.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment