Ability To Use Dynamic Shortcodes In Headline Block Within GB Query Loop
At least until there is adequately advanced integration with Metabox custom fields in GBP, it would be helpful if I could use dynamic data shortcodes in headline blocks. For example, I want to pull a date or time from a custom field created in Metabox. But I want to display that date or time in a custom format. One way to do that would be to use a Metabox shortcode that includes the format I want it displayed in (for example, "F j,Y"). I am able to use a dynamic shortcode when creating a single post element in GP, but the shortcode option doesn't seem to be working when I try to use it in the new GB Query Loop.
See Loom Video for more info: https://www.loom.com/share/c38c0c24311e4ebd8f5b5642676e2e40
Hi there, I believe this issue has been raised for GB Pro. Meanwhile, I believe metabox should probably work similarly with ACF meaning that the value is always saved as Ymd (YYYYMMDD) in the database. In which case, you would need to process the value and insert the appropriate shortcode like so:
function display_my_date() {
$my_date = get_field('event_start_date');
ob_start();
if ( $my_date ): $date = date("I F j, Y", strtotime($my_date));?>
<?php echo $date; ?>
<?php endif;
return ob_get_clean();
}
add_shortcode( 'my_date', 'display_my_date' );
In this case, you don't need to activate dynamic data, just insert the above shortcode on the headline.
But my issue (unless I am doing something wrong) is that dynamic shortcodes don't seem to be displaying data in the query loop. Could this be because simply inserting a shortcode into a headline block is not tying that shortcode to pull from the current post?
@rockyviewweb I've just tested it, you are right, headline shortcodes are not rendered inside the query loop, What you could do is enable dynamic data for the headline, select Post Meta as Data type and enter the custom field name in the Post meta field input field.
https://i.imgur.com/flEpwbi.png
Then, depending on the date output you would need to programmatically update the metabox date value to another format, possibly by consulting the Metabox documentation.
@tomusborne
I wonder if there is a technical reason why a dynamic shortcode wouldn't be rendering correctly within the query loop. They do work in a GP single post element. Is it just a simple change in the plugin code to get them to also work in the query loop? Or is it more complicated?
I tried another (non-dynamic field) related shortcode in a query loop headline block, and that did seem to display. So the issue seems to be something to do with shortcodes based on dynamic/custom fields not rendering correctly.
Strange, shortcodes should render normally as they do outside of the Query Loop block. I wonder if the shortcode itself is having trouble within the given context (the loop).
However, adding the big shortcode like that looks super ugly, so it's not a great solution anyways.
The best thing here would be to be able to filter the output on the frontend so you could overwrite the get_post_meta() value with what you need. We don't have filters for this right now, but I'll look at it for beta.2.
Thanks!
Filters would be amazing - if I am understanding correctly. So I could call up a post meta field for a date, then apply a filter to format the date in the way I want (like F j, Y)?
Exactly, you would basically overwrite the value returned by GenerateBlocks with your own custom value. Better integration with these kinds of things is the goal, but filters will be needed anyways and will temporarily solve issues like this.
The filters and meta box integration will be handled by https://github.com/tomusborne/generateblocks-pro/issues/102