Support

Account

Home Forums Add-ons Repeater Field Need your help about have_rows function

Solving

Need your help about have_rows function

  • Hi Team,
    I got a problem with have_rows function need your help!
    I used a Pods plugin to create a custom post type name “tour”. And I used advanced custom fields plugin to create the repeater field and a few sub fields for custom post type “tour”.
    I created a new page which show a list of the tours.It done!
    Then I created a shortcode to show the sub fields in the repeater of every the tour.
    This is my shortcode:

    
    /**
     * Get the destination on every tour
     */
    if (!function_exists('get_destination_sc')) {
    	function get_destination_sc($atts) {
    		$atts = shortcode_atts(array(
    			'id' => NULL,
    		), $atts);
    
    		// check if the repeater field has rows of data
    		if( have_rows('day_itinerary', $atts['id']) ):
    
    		 	// loop through the rows of data
    		    while ( have_rows('day_itinerary', $atts['id']) ) : the_row();
    		        // display a sub field value
    		        the_sub_field('day_destination');
    		    endwhile;
    		else :
    		    // no rows found
    			print_r('no rows found');
    		endif;		
    	}
    	add_shortcode('destination', 'get_destination_sc');
    }
    
    

    I putted this shortcode in two places.One in the page which I’ve said above. One in the single tour template file.
    But only the shortcode in single template is working.
    I didn’t know why the shortcode in the page didn’t working.
    Please help me check this.
    Cheers.

  • Hi @dagiac

    All ACF functions will load and save data to a $post. This is an important concept to understand.

    The shortcode will work on your single template, buecause WP understands there that all the info on that page is related to a single $post, and ACF can load / save data to that $post.

    On your ‘archive’ page, It is most likely that ACF does not know which $post to load the data from.
    Is the ‘archive’ page looping through the ‘tour’ posts?
    For each post, is it using the the_content() function to output text?
    If the_content() is not being used within the loop, the shortcode will never be called. You can remove the ACF code from your shortcode function and simply try to output some data about the $post, thus removing the potential issue of ACF.

    Once you can confirm that the shortcode is running in the correct template, and that the $post data is correct, you can add back in the ACF code.

    Thanks
    E

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

You must be logged in to reply to this topic.