• Hi,

    I want to make a loop display only the posts which have any ACF flexible value.

    Currently, I am using this code:

    [pass global=route]
    [loop type=case taxonomy=arbitrator term='{FIELD_2}']<br>
    [if field="damages"]
    	<div class="case-content case-dama">
    [field title-link] 
    		<h2>DAMAGES</h2>
    		
    		[flex damages]
    		[layout fet-text]
    		<p class="single_acf_text">[field fet-text]</p>
    			[/layout]
    	
    		[layout fet-select]
    		<p class="single_acf_select">[field fet-select]</p>
    		[/layout]
    		[/flex]
    	</div>
    	[/if]  [/loop] [/pass]

    This code displays all the posts which have any value in damages ACF Flexible Content field, such as:

    POST_1
    DAMAGES
    Flexible Content
    
    POST_2
    DAMAGES
    Flexible Content
    
    POST_3
    DAMAGES
    Flexible Content

    What I need is:

    DAMAGES
    
    POST_1
    Flexible Content
    
    POST_2
    Flexible Content
    
    POST_3
    Flexible Content
    

    Is there any method to proceed with a loop only if a certain field exists?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Use [loop exists] and [the-loop]

    Thread Starter mehmetba

    (@mehmetba)

    @polarracing thanks for your answer. You’ve been very helpful as always.

    The following code worked, with a caveat:

    [pass global=route] [loop exists type=case taxonomy=arbitrator term='{FIELD_2}']
    	DAMAGES<br>
    	[the-loop] 
    	[if field="damages"] 
    	[field title-link] 
    
    	[flex damages] 
    
    	[layout fet-text]
    	<p class="single_acf_text">[field fet-text]</p>
    	[/layout] 
    	
    	[layout fet-select]
    	<p class="single_acf_select">[field fet-select]</p>
    	[/layout]
    	
    	[/flex] 
    	
    	[/if] 
    	[/the-loop] 
    	[/loop] 
    	[/pass]

    This works for one field. However I want to run this code for all of the flexible content fields. I was thinking adding the same code with different fields, but [loop exists] solution means tens of nested loops.

    Could you please think of any other way?

    What you mean it works for one field?

    Within the loop you can add as many fields you like.

    • This reply was modified 3 years, 9 months ago by polarracing.
    Thread Starter mehmetba

    (@mehmetba)

    For example, this is what’s required:

    DAMAGES
    
    POST_1
    Damages Flexible Content
    
    POST_2
    Damages Flexible Content
    
    POST_3
    Damages Flexible Content
    
    ______________________
    
    ADMISSIBILITY
    
    POST_1
    Admissibility Flexible Content
    
    POST_2
    Admissibility Flexible Content
    
    POST_3
    Admissibility Flexible Content

    There are tens of ACF Flexible Content fields for a post, some are empty, some are not.

    I guess I can add every ACF Flexible Content Field through if clauses inside the-loop, however I couldn’t figure out how to hide the title when there field is empty and isn’t displayed.

    • This reply was modified 3 years, 9 months ago by mehmetba.
    • This reply was modified 3 years, 9 months ago by mehmetba.

    You can run the same code multiple times with different parameters.

    Just use [pass list=’fieldname1,fieldname2′] and wrap your code within.

    Thread Starter mehmetba

    (@mehmetba)

    Thanks again for your engagement. I’ve tried the following, to no avail. Displays nothing.

    [pass global=route] [-pass list='damages,admissibility'] 
    [loop exists type=case taxonomy=arbitrator term='{FIELD_2}']<br>
    <br>
    [the-loop] 
    
    [if field={ITEM}] 
    
    [field title-link]
    
    [flex {ITEM}] 
    [layout fet-text]
    <p class="single_acf_text">[field fet-text]</p>
    [/layout] 
    
    [layout fet-select]
    <p class="single_acf_select">[field fet-select]</p>
    [/layout] 
    [/flex] 
    [/if]
    [/the-loop] 
    [/loop] 
    [/-pass] 
    [/pass]

    Without a deeper look.

    [flex {ITEM}] is not correct in this case as you pass the list with [-pass] – it has to be {-ITEM}.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Loop if an ACF flexible field is not empty’ is closed to new replies.