Support

Account

Home Forums ACF PRO Create a widget with ACF

Solved

Create a widget with ACF

  • 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

    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!

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Create a widget with ACF’ is closed to new replies.