• Resolved Chris

    (@osiak)


    Hi,
    I use ACF Pro repeater field to add custom fields to the user profile page. Now I’d like to output that data on the frontend (using a shortcode). However I cannot make this happen. When I place the repeater field on a page it shows up nicely – but not on the user’s page (location: user form > equal > add/edit). This is what I have:

    function acf_repeater() {
      um_fetch_user( get_current_user_id() ); {
       if( have_rows('repeater-name') ):
        while ( have_rows('repeater-name') ) : the_row();
         the_sub_field('sub-field-name');
        endwhile;
       else :
      endif;
     }	
    }
    add_shortcode('acf_repeater_shortcode', 'acf_repeater');

    Any tips?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @osiak

    Could you please try this code snippet:

    function acf_repeater() {
       $user_id = get_current_user_id();
    
       if( have_rows('repeater-name',"user_{$user_id}" ) ):
        while ( have_rows('repeater-name', "user_{$user_id}" ) ) : the_row();
         the_sub_field('sub-field-name');
        endwhile;
       else :
      endif;
     }	
    }
    add_shortcode('acf_repeater_shortcode', 'acf_repeater');

    Regards,

    Thread Starter Chris

    (@osiak)

    This showed the output on top of the page. This modification puts it in the right place:

    function acf_repeater() {
    
       $user_id = get_current_user_id();
    ob_start();
       if( have_rows('repeater-name',"user_{$user_id}" ) ):
        while ( have_rows('repeater-name', "user_{$user_id}" ) ) : the_row();
         the_sub_field('sub-field-name');
        endwhile;
       else :
      endif;
    $output = ob_get_clean();
        return $output;
    }
    add_shortcode('acf_repeater_shortcode', 'acf_repeater');

    However when I have more than one row in the repeater, the subfields appear one ofter another. Now I have a problem with making it somehow appear as a table, especially that I want to output more subfields.

    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @osiak

    Could you please provide a screenshot of the current output? If possible, a short clip so I can see the issue and try to replicate it on our end.

    Regards,

    Thread Starter Chris

    (@osiak)

    Ok, I expanded on the code using the Advanced loop described here: https://www.advancedcustomfields.com/resources/repeater/

    I wanted to have the output as a nice table, similar to the backend.
    This is what I used which worked beautifly (in my code I added classes to the table to style it):

    function acf_repeater() {
    
       $user_id = get_current_user_id();
       ob_start(); ?>
       <?php if( have_rows('repeater-name',"user_{$user_id}" ) ): ?>
    
    	<table>
    	   <tr>
    	   	<td>Header1:</td><td">Header2:</td><td>Header3:</td><td>Header4:</td>
    		</tr>
    
    	<?php while ( have_rows('repeater-name', "user_{$user_id}" ) ) : the_row(); 
    
    		// vars
    		$subfieldname1 = get_sub_field('sub-field-name-1');
    		$subfieldname2 = get_sub_field('sub-field-name-2');
    		$subfieldname3 = get_sub_field('sub-field-name-3');
    		$subfieldname4 = get_sub_field('sub-field-name-3');
    
    	?>
    	   <tr>
    	      <td><?php echo $subfieldname1; ?></td><td><?php echo $subfieldname2; ?></td><td><?php echo $subfieldname3; ?></td><td><?php echo $subfieldname4; ?></td>
    	   </tr>
    		
    
    	<?php endwhile; ?>
    
    	</table>
    
    <?php endif; ?>
    <?php $output = ob_get_clean();
        return $output;
    }
    add_shortcode('acf_repeater_shortcode', 'acf_repeater');
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @osiak

    Thanks for sharing the solution! I am closing this thread now.

    Regards,

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Output ACF repeater on user’s profile page’ is closed to new replies.