Support

Account

Home Forums Add-ons Repeater Field shortcode with parameters for repeater field

Helping

shortcode with parameters for repeater field

  • I am using ACF repeater field for making a custom menu, so I can use different menu’s on each product description page. I have added a options page to allow the user add details from the dashboard. Screenshot here.
    ACF repeter field, options page

    I have a image field, a nav menu, for which I am using this plugin(https://github.com/jgraup/advanced-custom-fields-nav-menu-field) and a couple of more fields.

    I am looking for a solution to have a short-code to spit out only one row out of the list something like [custom_menu-1] for the first one. (OR should I use 0 since the repeater array starts with 0?)

    This is what I am currently using to display a preview on the dashboard.

    	/**
    	 *  Display custom content after an ACF settings page
    	 *
    	 *  @since 1.0.0
    	 */
    	public function after_acf_options_page()
    	{
    		if( have_rows( 'repeter_field_name', 'option' ) ) :
    
    			while( have_rows( 'new_local_menu', 'option' ) ) :
    
    			the_row(); ?>
    
    				<nav id="localnav" class="js no-touch css-sticky" lang="en-US" dir="ltr" data-sticky="" data-analytics-region="local nav" role="navigation">
    
    					<div class="ln-wrapper">
    
    						<div class="ln-background"></div>
    
    						<div class="ln-content">
    
    							<div class="ln-title">
    
    								<?php if( '' !== get_sub_field( 'logo' ) ) : 
    								$secondary_logo = get_sub_field('logo');
    									if (!empty($logo)) {
    										printf( '<a href="%s" data-analytics-title="secondary logo"><img alt="logo" src="%s" height="50px"/></a>',
    										get_sub_field( 'logo_hyperlink' ),
    										get_sub_field( 'logo' )
    									);
    										   }
    										   ?>
    
    								<?php endif; ?>
    
    							</div><!-- /.ln-title -->
    
    							<div class="ln-menu">
    
    								<a href="#ln-menustate" class="ln-menucta-anchor ln-menucta-anchor-open" id="ln-menustate-open">
    									<span class="ln-menucta-anchor-label">Open menu</span>
    								</a>
    								<a href="#" class="ln-menucta-anchor ln-menucta-anchor-close" id="ln-menustate-close">
    									<span class="ln-menucta-anchor-label">Close menu</span>
    								</a>
    
    								<?php the_sub_field( 'select_nav_menu' ); ?>
    
    								<?php if( '' !== get_sub_field( 'call_to_action' ) ) : ?>
    
    									<div class="ln-action ln-action-button">
    										<?php printf( '<a class="ln-button" href="%s" data-analytics-title="button">%s <span class="ln-action-product"></span></a>',
    											esc_url( get_sub_field( 'call_to_action' ) ),
    											__( 'Buy' )
    										); ?>
    									</div><!-- /.ln-action-button-->
    
    								<?php endif; ?>
    
    							</div><!-- /.ln-menu -->
    
    						</div><!-- /.ln-content -->
    
    					</div><!-- /.ln-wrapper -->
    
    				</nav><!-- /#localnav -->
    
    			<?php endwhile;
    
    		endif;
    	}

    Obviously, it display all the rows that are added so far. How do I identify each row? I read that each repeater field row had an id starting with 0, like an array. Assuming that I can use that to identify which row to display. I am not sure what should I change in my code to do so.

    I read a few other articles and topics here, but most of them are use to display all the rows.
    Can someone please help me with this.

  • The repeater field name is constructed by combining the parent field name (the repeater) the row index (yes it starts at 0) and the sub field name.

    "{$repeater_name}_{$index}_{$sub_field_name}"

    But even with this you can’t use the built in ACF shortcode. The reason is that the ACF shortcode will only work with basic fields that store simple text values and will not work for fields like image, nav menu, and many other fields that store values that need to be interpreted by ACF. For this type of thing you’re going to have to create your own shortcodes https://codex.wordpress.org/Shortcode_API

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

The topic ‘shortcode with parameters for repeater field’ is closed to new replies.