Support

Account

Home Forums Add-ons Repeater Field Hide a repeating audio field when empty

Solving

Hide a repeating audio field when empty

  • Hi I am trying to hide a repeating audio field when empty and can’t get it right. Please can someone help. This is the code in the loop-index.php

    $story_audio = '<div class="story-audio">';
    $story_audio .=  '<span><strong>Audio / Voice recordings: </br></strong></span>';
    	if( have_rows('voice') ): while ( have_rows('voice') ) : the_row();
    	$file = get_sub_field('voice_recording');
    	$story_audio .='<a href="'. $file['url'].'" target="_blank">'.$file['title'].'</a></br>';
    
    	endwhile; else : endif;
    $story_audio .= '</div>';
    if (function_exists('zbp_content')) {
    		$story_audio = zbp_content($story_audio);
    }
    $content_output .= $story_audio;
    $content_output .= '<div class="story-links">';
    $content_output .=  '<span></br><strong>Links to related web content / sources: </br></strong></span>';
    	if( have_rows('sources') ): while ( have_rows('sources') ) : the_row();
    	$content_output .='<a href="'. get_sub_field('link_url').'" target="_blank">'.get_sub_field('site').'</a></br>';
    	endwhile; else : endif;
    $content_output .= '</div>';
    	endif;

    Thanks
    Rob

  • Hi @fanlokbun

    You could make use of a conditional on this line to check if a value exists:

    $file = get_sub_field('voice_recording');
    if($file){
    	$story_audio .='<a href="'. $file['url'].'" target="_blank">'.$file['title'].'</a></br>';
    //set all the logic within this
    }
  • Thank you very much James. I’m a bit green at this. I can’t seem to get it right. What would be the logic needed to hide if empty?
    Thanks again
    Rob

  • Hi @fanlokbun ,

    Thanks for the follow up.

    By simply executing the code inside the if control where the voice recording is not empty, it means that the link will only show when its not empty otherwise hidden.

    If you are still not sure, feel free to let me know.

  • That’s what I thought. I must have done something wrong in the code. This is the code with the if statement. Can you see why it doesn’t hide when empty?
    Thanks
    Rob

    $story_audio = '<div class="story-audio">';
    $story_audio .=  '<span><strong>Audio / Voice recordings: </br></strong></span>';
    	if( have_rows('voice') ): while ( have_rows('voice') ) : the_row();
    	$file = get_sub_field('voice_recording');
    	if($file){
    	$story_audio .='<a href="'. $file['url'].'" target="_blank">'.$file['title'].'</a></br>';
    	//if statement added from ACF - set all the logic within this
    	}
    
    	endwhile; else : endif;
    $story_audio .= '</div>';
    if (function_exists('zbp_content')) {
    		$story_audio = zbp_content($story_audio);
    }
    $content_output .= $story_audio;
    $content_output .= '<div class="story-links">';
    $content_output .=  '<span></br><strong>Links to related web content / sources: </br></strong></span>';
    	if( have_rows('sources') ): while ( have_rows('sources') ) : the_row();
    	$content_output .='<a href="'. get_sub_field('link_url').'" target="_blank">'.get_sub_field('site').'</a></br>';
    	endwhile; else : endif;
    $content_output .= '</div>';
    endif;
  • Hi James, Have you had a chance to check my code yet?
    Many thanks
    Rob

  • Hi @fanlokbun

    Apologies for the delay.

    You need to remove the endif; at the bottom of your code.

    This should work:

    $story_audio = '<div class="story-audio">';
    $story_audio .=  '<span><strong>Audio / Voice recordings: </br></strong></span>';
    	if( have_rows('voice') ): while ( have_rows('voice') ) : the_row();
    	$file = get_sub_field('voice_recording');
    	if(get_sub_field('voice_recording')){
    	$story_audio .='<a href="'. $file['url'].'" target="_blank">'.$file['title'].'</a></br>';
    	//if statement added from ACF - set all the logic within this
    	}
    
    	endwhile; else : endif;
    $story_audio .= '</div>';
    if (function_exists('zbp_content')) {
    		$story_audio = zbp_content($story_audio);
    }
    $content_output .= $story_audio;
    $content_output .= '<div class="story-links">';
    $content_output .=  '<span></br><strong>Links to related web content / sources: </br></strong></span>';
    	if( have_rows('sources') ): while ( have_rows('sources') ) : the_row();
    	$content_output .='<a href="'. get_sub_field('link_url').'" target="_blank">'.get_sub_field('site').'</a></br>';
    	endwhile; else : endif;
    $content_output .= '</div>';
  • Hi James,
    I tried just removing the endif; but it broke the page. This is all the code added to the loop-index. What I want is any empty field to be hidden. I concentrated on the voice audio because there are very few so it is most important to hide.
    Thanks
    Rob

    // BEGIN CHRIS CUSTOM
                    $content_output  =  '<div class="entry-content" '.avia_markup_helper(array('context' => 'entry_content','echo'=>false)).'>';
                    $content_output .=  $content;
    
    if($current_post['post_type'] == "story"):
    
                    $date = get_field('contributed_on'); 
                    $y = substr($date, 0, 4);
                    $m = substr($date, 4, 2);
                    $d = substr($date, 6, 2);
                    $time = strtotime("{$d}-{$m}-{$y}");
                    $images = get_field('image_gallery');
    
    				/* $content_output .= '<div class="story-id">';
    				$content_output .=  '<span><strong>Story ID: </strong></span>'.get_post_meta( $post->ID, 'story_id', true).
    				'</br>';
                    $content_output .= '</div>';
    				
    				$content_output .= '<div class="story-contributor">';
                    $content_output .=  '<span><strong>Contributor: </strong></span>'.get_post_meta( $post->ID, 'contributor', true).'</br>';
                    $content_output .= '</div>';
                    
    				$content_output .= '<div class="story-date">';
    				$content_output .=  '<span><strong>Contributed on: </strong></span>'.date('F dS Y', $time).'</br>';
                    $content_output .= '</div>';
    				
    				$content_output .= '<div class="story-permission">';
    				$content_output .=  '<span><strong>Permission to publish: </strong></span>'.get_post_meta( $post->ID, 'permission', true).'</br>';
    				$content_output .= '</div>'; */
                   
    			   $content_output .= '<div class="story-surname">';
    			   $content_output .=  '<span><strong>Surname: </strong></span>'.get_post_meta( $post->ID, 'persons_surname', true).'</br>';
    			   $content_output .= '</div>';
    
    			   $content_output .= '<div class="story-other-names">';
    			   $content_output .=  '<span><strong>Other names: </strong></span>'.get_post_meta( $post->ID, 'other_names', true).'</br>';
    			   $content_output .= '</div>';
    
                   $content_output .= '<div class="story-related">';
                   $content_output .=  '<span><strong>Other people in this story: </br></strong></span>';
    					if( have_rows('related_people') ): while ( have_rows('related_people') ) : the_row();
    					$content_output .=  get_sub_field('first_name').' '.get_sub_field('surname').'</br>';
    					endwhile; else : endif;
                   $content_output .= '</div>';
    
    			   $content_output .= '<div class="story-places">';
                   $content_output .=  '<span><strong>Locations in this story: </br></strong></span>';
    					if( have_rows('place') ): while ( have_rows('place') ) : the_row();
    					$content_output .=  get_sub_field('place').'</br>';
    					endwhile; else : endif;
    				$content_output .= '</div>';
    
    				$content_output .= '<div class="story-text">';
    				$content_output .=  '<span></br><strong>Story: </br></strong></span>'.get_post_meta( $post->ID, 'story', true).'</br>';
    				$content_output .= '</div>';
    
    				$content_output .= '<div class="story-images">';
    				$content_output .=  '<span></br><strong>Images: </br></strong></span>';
    					if( $images ): 
    					$content_output .=  '<ul>';
    						foreach( $images as $image ):
    						$content_output .= '<li>';
    						$content_output .= '<a class="lightbox" href="'.  $image['url'].'">';
    						$content_output .= '<img src="'. $image['sizes']['thumbnail'].'" alt="'.  $image['alt'].'" title="'. $image['title'].'" />';
    						$content_output .= '</a>';
    						$content_output .= '</li>';
    						endforeach;
    					$content_output .= '</ul>';
    					endif; 
    				$content_output .= '</div>';
    
    				$story_audio = '<div class="story-audio">';
    				$story_audio .=  '<span><strong>Audio / Voice recordings: </br></strong></span>';
    					if( have_rows('voice') ): while ( have_rows('voice') ) : the_row();
    					$file = get_sub_field('voice_recording');
    					if($file){
    					$story_audio .='<a href="'. $file['url'].'" target="_blank">'.$file['title'].'</a></br>';
    					//if statement added from ACF - set all the logic within this
    					}
    
    					endwhile; else : endif;
    				$story_audio .= '</div>';
    				if (function_exists('zbp_content')) {
    						$story_audio = zbp_content($story_audio);
    				}
    				$content_output .= $story_audio;
    				$content_output .= '<div class="story-links">';
    				$content_output .=  '<span></br><strong>Links to related web content / sources: </br></strong></span>';
    					if( have_rows('sources') ): while ( have_rows('sources') ) : the_row();
    					$content_output .='<a href="'. get_sub_field('link_url').'" target="_blank">'.get_sub_field('site').'</a></br>';
    					endwhile; else : endif;
    				$content_output .= '</div>';
    endif;
    
    // END CHRIS CUSTOM
Viewing 8 posts - 1 through 8 (of 8 total)

The topic ‘Hide a repeating audio field when empty’ is closed to new replies.