Support

Account

Home Forums General Issues Warning whith Relationship Fields

Solved

Warning whith Relationship Fields

  • Hi !
    I have a problem with Relationship Fields : sidebar_widget
    In my page.php like in the documentation

    	<?php 
     
    $posts = get_field('sidebar_widget');
     
    if( $posts ): ?>
    
    	<?php foreach( $posts as $p ): // variable must NOT be called $post (IMPORTANT) ?>
    	    <?php include('widget/content-sidebar-widget.php'); ?>
    	<?php endforeach; ?>
    
    <?php endif; ?>

    and
    Warning: Invalid argument supplied for foreach() in
    what can i do ?

    wordpress 3.8

  • Hi @[email protected]

    Please debug your data like so:

    
    echo '<pre>';
    	print_r( $posts );
    echo '</pre>';
    die;
    

    Thanks
    E

  • Hi !
    I find this solution with query !
    This code work perfectly.

    ` $sidebar = get_field(‘sidebar-widget’);
    $args = array(
    ‘post_type’ => ‘sidebar-widget’,
    ‘page_id’ => $sidebar
    );
    $query = new WP_Query( $args );
    // The Loop
    ?>
    <?php if($query->have_posts()) : ?>
    <?php while($query->have_posts()) : ?>
    <?php $query->the_post(); ?>
    <?php include(‘widget/content-sidebar-widget.php’); ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>`

  • Sorry,
    The true solution

    <?php 
    	$posts = get_field('sidebar_widget');
    			$args = array(
    			'post_type' => 'sidebar-widget',
    			'name'=> ($posts->post_name), //get the value in array
    			);
    			$query = new WP_Query( $args );
    
    			// The Loop
    			?>													
    			<?php if($query->have_posts()) : ?>
    				<?php while($query->have_posts()) : ?>
    				<?php $query->the_post(); ?>
    					<?php include('widget/content-sidebar-widget.php'); ?>
    				<?php endwhile; ?>
    				<?php wp_reset_postdata(); ?>
    			<?php endif; ?>
    			
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Warning whith Relationship Fields’ is closed to new replies.