A wordpress website with the advanced custom fields plugin installed. There is a need to create your own widget, which include some fields from advanced custom fields . I create a widget in this way, including arbitrary fields in it (image output)

class CustomWidget extends WP_Widget { function __construct() { parent::__construct( 'custom_widget', // Base ID __('CustomWidget', 'text_domain'), // Name array( 'description' => __( 'Some CustomWidget', 'text_domain' ), ) ); } // widget actual processes public function widget( $args, $instance ) { echo "<p>custom widget appear</p>"; echo "<p>". the_field('widget_banner') . "</p>"; } // outputs the content of the widget public function form( $instance ) {} // outputs the options form on admin public function update( $new_instance, $old_instance ) {} // processes widget options to be saved } // register Foo_Widget widget function register_foo_widget() { register_widget( 'CustomWidget' ); } add_action( 'widgets_init', 'register_foo_widget' ); 

the line custom widget appear is displayed, but the arbitrary field the_field('widget_banner') does not exist, although it is in the admin area, I fill it in and click save, and it is saved

I am doing a pattern on the forum , but the guys in the comments have the same problem

This is what argues in the frontend

 array(10) { ["name"]=> string(14) "Виджеты" ["id"]=> string(9) "sidebar-1" ["description"]=> string(36) "Добавьте сюда блоки" ["class"]=> string(0) "" ["before_widget"]=> string(64) " " ["after_widget"]=> string(8) " " ["before_title"]=> string(4) " " ["after_title"]=> string(5) " " ["widget_id"]=> string(15) "custom_widget-2" ["widget_name"]=> string(12) "CustomWidget" } 

    1 answer 1

    Here is a description of how to create a Custom Widgets Widget API , to add it, you must also create add_action()

      add_action( 'widgets_init', function(){ register_widget( 'My_Widget' ); }); 

    Your widget , form and update will contain a specific code.

    Is this your post ? There I explained how to display

    In the widget you can display this field or this:

      <?php the_field('widget_banner'); ?> 

    or so:

      <?php echo get_post_meta(get_the_ID(), 'widget_banner', true) ?> 

    provided that you have created this widget_banner field, which is tied to post_type -> post and output it in a loop, see the example in the picture (line 69-70):

    enter image description here

    result:

    enter image description here

    Admin panel

    enter image description here

    • Yes, I forgot to include it in the description - I also have this unit, but it also does not display anything with it - Vasya
    • Try first with static text, and then you will be outputting from CF, replace the_field('widget_banner') with static content for now - eugene_v
    • Please see the description carefully - static content displays - echo "<p>custom widget appear</p>"; I'm talking about this - Vasya
    • Yes, sorry I did not see. So the widget works for you, you just incorrectly transfer the parameters from CF to it - eugene_v
    • If I'm not mistaken, did you previously ask about the output of data from Pods? - eugene_v