Hi,
I updated to ACF PRO and I discover that it has support for widget! Simply awesome! This plugin is becoming really a must. I love it with all my soul and body.
Ok, I created a new custom widget using the code in Example paragraph of the WordPress docs:
http://codex.wordpress.org/Widgets_API
Then I added with ACF a pair of custom fields (text, image) to the widget and I see them inside widget.
Now, what I need to do for showing the custom fields in the frontend?
Hi @donad
Please see the documentation here: http://www.advancedcustomfields.com/resources/get-values-widget/
Thanks
E
Hi
I am a little lost on this, even with the linked page above.
I too have created a custom widget within my theme which uploads an image to echo out onto the sidebar.
I’m not sure how to check if the image has been uploaded and then if so, display the image. Do I need to add the code(and change it to suit) in my functions.php file?
I added the following to my widget class:
if ( get_field('upload_image', 'widget_' . $widget_id); ) {
echo 'IMAGE!!';
}
Thanks in advance
Figured out the answer, see below. This snippet is added to the widget class in my custom widget file.
if (get_field('upload_image', 'widget_' . $widget_id)) {
$attachment_id = get_field('upload_image', 'widget_' . $widget_id);
$size = "full";
$image = wp_get_attachment_image_src( $attachment_id, $size );
$alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
echo '<img alt="'.$alt.'" src="' . $image[0] . '" />';
}
@huwrowlands if you make widgets a lot, check out http://acfwidgets.com
Could speed up your workflow quite a bit.
Cheers!
The topic ‘Create a widget with ACF’ is closed to new replies.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.