Get ACF field in Structural builder custom layout
-
Hello,
Is it possible to get ACF field when using Structural builder ?
I can get it when using classic custom layout, but :
- I like the way the data in Structural Builder are displayed
- if I want to copy this way, I have to copy too much HTML to render correctly
- I have only 1 ACF field to retrieve !
Thanks for your help, and thank you for your amazing plugin !
-
Hi!
Of course, you can get the ACF field values ββwithin Structural Builder. To do this, select the Start from Scratch tab in the settings. In the list of elements, look for a component named Custom Field (ACF). Then, select your custom field from the list.Thanks for your very quick answer π
Your answer is almost perfect, the rendering is exactly what I’m needing ! But I need to do some extra processing on my ACF dates (I have in fact 2 dates, 1 for the beginning of an event, 1 for the ending, and I have to process it to properly display it) Do you think it’s possible ? How to do that ?
Many thanks !
You mean getting dates from ACF and then inserting them into the post card, right? If so, it’s probably better to process them using a shortcode (format the desired date) and then insert the shortcode into the post card. There’s a Shortcode component that inserts and displays the data.
Thank you for your reply.
I have already tried this way, but I’m not able to get the ACF field from the global $wp_query within functions.php : the have_posts() functions return 0.
How do you get the current ymc query ?
Many thanks
To get the ID of a page or post in functions.php, use the global $post variable (within the context), the get_queried_object_id() function (most reliable), or get_the_ID() (if used inside a loop).
The main ways to get the ID are: get_queried_object_id(): A universal method that works in templates, archives, and single pages, returning the ID of the current request. $post->ID: Requires explicit declaration of global $post; before use. url_to_postid(): Allows you to get the ID based on the URL, if known.
Getting the ID of the current page/post (universal):function get_current_page_id() {$id = get_queried_object_id(); return $id; } add_action('wp_head', 'get_current_page_id');
ORfunction check_post_id() { global $post; if ( is_single() || is_page() ) {echo $post->ID; } }add_action('wp_head', 'check_post_id');Using get_the_ID() (only inside the WordPress loop):
// If the code is called when generating content
$current_id = get_the_ID();It is better to use get_queried_object_id() or the global $post variable.
I’m afraid that doesn’t work for me.
I added a shortcode field [processacfdate] in card within structural builder, then in functions.php :
function process_acf_date() {
$id = get_queried_object_id();
echo “post Id = ” . $id . “<br/>”;
}
-> displayed “post Id : 0” π
The other global $post variable display nothing π
If you use a DataPicker field in ACF attached to a post, you can display this field in the post card using a component called Custom Field (ACF) (see screenshot). The plugin already supports these field types, and this one displays perfectly on the frontend in the post card itself. We think this is what you meant.
Yes, I am able to display perfectly an ACF field, like Data Picker. But I have to make some processes on it, not just display the field.
Have you an idea why I’m not able to access the post id through global $post or global $wp-query within a shortcode ?
Many thanks
Hi!
You can get the post ID within the shortcode itself. See example:add_shortcode( 'test_sc', function($atts) {
$atts = shortcode_atts( array(
'post_id' => get_the_ID(),
), $atts );
$post_id = intval($atts['post_id']);
return '<div>
The test shortcode works successfully! Post ID: '. $post_id .'
</div>';
});Then in the builder, insert the shortcode names (see screenshot)
https://prnt.sc/wz6oQqBto-4b
Thus, inside the shortcode you have the opportunity to get the post ID and make some calculations) We hope this helps you!Yes, perfect ! I didn’t know shortcode_atts() function, it is the one to know π
Thank you very much
Good Luck!
You must be logged in to reply to this topic.