Support

Account

Home Forums Front-end Issues If repeater field in functions.php

Solving

If repeater field in functions.php

    • wozzal

    • January 7, 2020 at 3:03 am

    Hi Guys

    I have a repeater field that I want to use to create a new product tab on woocommerce product page. I have got it to work well part from not displaying the tab if there are no fields. So I only want to show the tab if there are values in the repeater.

    I tried using the code below but this does not display the tab ever

    if(get_field('youtube_videos')):
    	add_filter( 'woocommerce_product_tabs', 'a_video_tab' );
    endif;

    Here is the full code for reference

    if(get_field('youtube_videos')):
    	add_filter( 'woocommerce_product_tabs', 'a_video_tab' );
    endif;
    	function a_video_tab( $tabs ) {
    		// Adds the new tab
    		$tabs['video'] = array(
    			'title'     => __( 'Videos', 'child-theme' ),
    			'priority'  => 70,
    			'callback'  => 'a_video_tab_callback'
    		);
    		return $tabs;
    	}
    	function a_video_tab_callback() {
    		echo '<div class="row">';
            	if( have_rows('youtube_videos') ):
    		while ( have_rows('youtube_videos') ) : the_row();
            $sub_value = get_sub_field('video_id');
            	echo '<div class="col-sm-6">';
    			echo '<div class="embed-responsive embed-responsive-16by9"><iframe class="embed-responsive-item" src="https://www.youtube.com/embed/';
    			the_sub_field('video_id');
    			echo '?rel=0" allowfullscreen></iframe></div>';		
    			echo '</div>';
    		endwhile;
    		else :
    		// no rows found
    		endif;
    		echo '</div>';
    
    	}
  • You’ll want to use the acf/input/admin_footer action to run jQuery for this. Also, I would create the tab that you want in the field group.

    Basically, you could look to see if there are rows in the body of that repeater field table then hide the tab if there are no rows . This script will get triggered when the edit post page is ready.

    It would look something like this:

    
    function check_for_videos() {
    
    ?>
    <script>
    var rowCount = jQuery('#acf-group_571814caa0f78 table tbody).find('tr');
    	if(rowCount.length == 0 ){
    		
    		jQuery('tab field selector here').hide();
    		
    }
    </script>
    <?
    }
    
    add_action('acf/input/admin_footer', 'check_for_videos');
    
    

    The most challenging part for me when doing this is getting the selector field correct to run the jQuery. If you’re not familiar with jQuery, you have to refer to the element that you want to run the script on and sometimes it’s hard to nail that down.

  • I think I accidentally deleted my response, so here it is again:

    I would use jQuery for this and also add the tab to the field group beforehand. You’ll want to use the acf/input/admin_footer action.

    It would look something like this:

    
    add_action('acf/input/admin_footer', 'check_for_videos');
    
    function check_for_videos(){
    
    ?>
    <script>
    
    var rowCount = jQuery('#acf-group_571814caa0f78 table tbody).find('tr');
    	if(rowCount ==0 ){
    		
    		jQuery('tab field selector here').hide();
    		
    	}
    </script>
    <?
    }
    
    

    It is sometimes difficult to nail down the exact name to use in the selector. For me it’s a trial and error thing.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.