-
necter
Hi there,
I am using the Events Calendar plugin and trying to create an Upcoming Events list with Query Loops that call up the event date. Is it possible to add the event date as a content source?
I have followed the instructions on this thread: https://generatepress.com/forums/topic/use-query-loop-to-display-the-events-calendar-events-on-home-page/, and the _EventStartDate custom field, but get an error when I try to publish the functions.php file.
Any thoughts?
-
Fernando
Hi Necter,
If you’re just grabbing the
_EventStartDate
, you shouldn’t need a custom code for that anymore. That feature is already available. Just set the Content Source to Post meta.Reference: https://docs.generateblocks.com/article/dynamic-data-options-overview/#content-post-meta
If you’re ordering by this meta however, what specific error are you getting from adding the code?
-
necter
Ah, I didn’t notice the ‘Add’ dropdown in the Post Meta Field box.
The output is in y-m-d format. Would you know the code to use in function.php to convert it to F j, Y date format?
Thanks Fernando.
-
Fernando
Here are a few steps:
1. Add
cu-convert-to-fjy
to the class list of the GB Headline Block. Adding Custom Classes: https://wordpress.com/support/wordpress-editor/adding-additional-css-classes-to-blocks/2. Then, add this snippet:
add_filter('generateblocks_dynamic_content_output', function($content, $attributes, $block){ if ( ! empty( $attributes['className'] ) && strpos( $attributes['className'], 'cu-convert-to-fjy' ) !== false ) { $timestamp = strtotime($content); $my_date = sprintf( '<time datetime="%1$s">%2$s</time>', date('c', $timestamp), date('F j, Y', $timestamp) ); return $my_date; } return $content; }, 10, 3);
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
-
necter
Hmm. I have added the custom class and code snippet but don’t see any change.
-
Fernando
I see. I updated the PHP code above. Can you try that instead?
-
necter
Brilliant! That’s the one. Thank you Fernando.
-
Fernando
You’re welcome, Necter!
-
Hi again Fernando,
I have just followed these steps on a different website but unable to achieve the same results. Any chance you maybe able to take a look for me, please?
Thank you in advance.
-
Hi there,
It looks like it’s working perfectly, the date format has been converted to
F j, Y
https://app.screencast.com/QhZ42C4XrPQlW -
I have updated the private information to include access.
-
Thanks for taking a look Ying. It looks like there were some duplicate events, which was causing the confusion with the events dates not displaying correctly. All good now.
-
Glad to hear that 🙂
- You must be logged in to reply to this topic.