Hi,
I’m trying to add ACF to a widget I have created and call it out where I need it. I have managed to create the widget and add the ACF fields to it I want, but the issue I’m having is calling those fields into the page from the widget.
My widget (Banner Widget) uses a Post Object field called ‘Banner Locations’. This is filtering by post type (Banner, a custom post type I have created). The return format is set to Post ID.
I can see the field within the widget I have created within the admin screens, but as I say I can’t call on the field.
I have looked at the documentation on how to call from a widget, but it doesn’t cover the post object being used here ( http://www.advancedcustomfields.com/resources/get-values-widget/ )
I have gone around in circles for hours, so I thought it best to layout the sort of thing I want to do.
add_filter(‘dynamic_sidebar_params’, __NAMESPACE__ . ‘\\my_dynamic_sidebar_params’);
function my_dynamic_sidebar_params( $params ) {
// get widget vars
$widget_name = $params[0][‘widget_name’];
$widget_id = $params[0][‘widget_id’];
// bail early if this widget is not a Banners widget
if( $widget_name != ‘Banners’ ) {
return $params;
}
/*
Get the post from the widget’s post object (ACF)
*/
/*
Query the post type for banners
*/
$bannerQuery = new WP_Query(array(“post_type” => “banner”, “posts_per_page” => 10, “banner_locations” => “footer_banners”));
if ($bannerQuery) { // if there are banners for the footer then lets show them
while ($bannerQuery->have_posts()) : $bannerQuery->the_post();
/*
More calls to ACF here from the post to show images text etc from repeater field
*/
endwhile; // End the loop. Whew.
}
wp_reset_query();
// return
return $params;
}
Please can anyone help me?
Thanks
Ash
First thing I noticed is that you are not referencing the global $post variable. Add this at the beginning of your function:
global $post;
Without that, using a loop that uses the_post()
will not get anything.
The code for getting a field from a widget is:
$my_value = get_field('field_name', 'widget_'.$widget_id);
Getting the fields from the banner posts, because you are doing a loop, is the same as getting a field anywhere else `get_field(‘field_name’);
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
ACF wouldn’t be so widely used in WordPress if it didn’t have some pretty amazing capabilities. In this article, we look at a few of the features we’ll discuss during “7 things you didn’t know you could do with ACF” at #WPEDecode later this month. https://t.co/5lnsTxp81j pic.twitter.com/Yf0ThPG1QG
— Advanced Custom Fields (@wp_acf) March 16, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.