Support

Account

Home Forums Front-end Issues Relationship Field in Widget

Helping

Relationship Field in Widget

  • I’m trying to create a widget included within a site that allows the user to select products from the store (WooCommerce) and display them in the sidebar.

    I’ve setup the field for the products to be selected on the individual page. I then have created a custom widget and registered the widget with the site. When the widget is placed in the sidebar, it is outputting the current page info (page title) instead of the selected items information.

    Link below will show you what I am taking about. In the sidebar you can see the data being output as “Tim Elmore” (title of current page). If you scroll to the bottom you will see the same code actually working, displaying the product titles.

    http://gldev.staging.wpengine.com/tim-elmore/

    Below is my code. Anyone have any idea why this won’t work within a widget but will if I enter it directly into the page template?

    /******* Products Widget *******/
     
     class WCProducts_Widget extends WP_Widget
    {
      function WCProducts_Widget()
      {
        $widget_ops = array('classname' => 'WCProducts_Widget', 'description' => 'Display products in sidebar' );
        $this->WP_Widget('WCProducts_Widget', 'Products', $widget_ops);
      }
     
        // widget form creation
      function form($instance)
      {
        if( $instance) {
         $title = esc_attr($instance['title']);
    } else {
         $title = '';
    }
    ?>
      <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
    <?php
      }
     
      function update($new_instance, $old_instance)
      {
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        return $instance;
      }
     
      function widget($args, $instance)
      {
        extract($args, EXTR_SKIP);
     
        echo $before_widget;
        $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
     
        if (!empty($title))
          echo $before_title . $title . $after_title;; ?>
     
    <?php 
     
    $posts = get_field('product_widget');
     
    if( $posts ): ?>
        <ul>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <span>Custom field from $post: <?php the_field('author'); ?></span>
            </li>
        <?php endforeach; ?>
        </ul>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    
    <?php 
     
        echo $after_widget;
      }
     
    }
    add_action( 'widgets_init', create_function('', 'return register_widget("WCProducts_Widget");') );
  • Disregard. I had to add <?php global $post; ?> to the code and it worked.

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

You must be logged in to reply to this topic.