Support

Account

Home Forums General Issues Optimising/reducing repetition across lengthy if/elseif statement

Helping

Optimising/reducing repetition across lengthy if/elseif statement

  • Hey there. Not sure if this is something someone can help guide me on, but I’d really appreciate if so.

    I have a block of code for my hero section, and that block of code is global across the site. In the code I check for what page/post type/archive it is, and depending on that then I load the options for that hero section from either the page/post or an options page. Almost everything gets repeated, but the one thing that usually changes is which group or repeaters I’m needing to go in to.

    Here is the general structure of the if/else statement. Inside each I then grab the ACF fields (hero title, background image, alignment, etc), but as far as I know I need to do this individually every single time:

    if ( is_singular() ):
    
    	if ( is_front_page() ):
    
    	if ( is_singular( 'ledige-stillinger' ) ):
            	if( have_rows('job_vacancies_single') ): 
                		while( have_rows('job_vacancies_single') ): the_row();
    
        	if ( is_cart() ):
            	if ( have_rows('cart_page', 'option') ): 
                		while ( have_rows('cart_page', 'option') ): the_row();
        
        	if ( is_checkout() ):
    	        if ( have_rows('checkout_page', 'option') ): 
                		while ( have_rows('checkout_page', 'option') ): the_row();
    				if( 'enable' === $intro_toggle && have_rows('intro_contents') ): 
                				while( have_rows('intro_contents') ): the_row();
    
        	if( have_rows('hero_options') ): 
        		while( have_rows('hero_options') ): the_row();
    
        	if ( have_rows('hero_contents') ): 
        		while ( have_rows('hero_contents') ): the_row();
    
        	if ( have_rows('intro_section') ): 
        		while ( have_rows('intro_section') ): the_row();
    			if( 'enable' === $intro_toggle && have_rows('intro_contents') ):
    				while( have_rows('intro_contents') ): the_row();
    
    elseif ( is_post_type_archive('kampanjer') ):
    	if ( have_rows('campaigns_archive', 'option') ): 
            	while ( have_rows('campaigns_archive', 'option') ): the_row();
    			if( 'enable' === $intro_toggle && have_rows('intro_contents') ): 
            			while( have_rows('intro_contents') ): the_row();
    
    elseif ( is_post_type_archive('service') ):  
    if ( have_rows('services_archive', 'option') ): 
            while ( have_rows('services_archive', 'option') ): the_row();
            	if ( 'enable' === $intro_toggle && have_rows('intro_contents') ): 
            		while ( have_rows('intro_contents') ): the_row();
    
    elseif ( is_post_type_archive('ledige-stillinger') ):
    
        if ( have_rows('vacancies_archive', 'option') ): 
            while ( have_rows('vacancies_archive', 'option') ): the_row();
    		if( 'enable' === $intro_toggle && have_rows('intro_contents') ): 
            		while( have_rows('intro_contents') ): the_row();
    
    elseif ( is_post_type_archive('nye-biler') ):
    
        if ( have_rows('new_cars_archive', 'option') ): 
            while ( have_rows('new_cars_archive', 'option') ): the_row();
    		if( 'enable' === $intro_toggle && have_rows('intro_contents') ): 
            		while( have_rows('intro_contents') ): the_row();
    
    elseif ( is_post_type_archive('ansatte') ):
        if ( have_rows('staff_archive', 'option') ): 
            while ( have_rows('staff_archive', 'option') ): the_row();
            	if( 'enable' === $intro_toggle && have_rows('intro_contents') ): 
            		while( have_rows('intro_contents') ): the_row();
    
    else

    Is there any way for me to reduce the repetition here?

  • I cannot tell what you need help with by looking at the code you provided, it seems incomplete. I assume you did not include everything because there would be a lot of code and I appreciate that, but it’s hard to tell from what was provided.

    The best that I can do is tell you what I do when I have to use a single component in multiple places.

    The first thing is that I try to use the same field group in all of these locations so that I can use all of the same field names. The problems is that with options pages that you need to have a way to separate the different options pages.

    When setting up an options page you can set the post_id that will be used for the fields on the options page. What is not well known is that you can set this to a text value. So for example

    Options page for 'vacancies_archive' the post ID for this options page could be set to 'vacancies_archive_options' and then the post ID for 'new_cars_archive' could be set to 'new_cars_archive_options'. What happens here is that the "option_name" value of these fields in the options page are prepended with this post ID value. ACF normally uses "options" so that fields in the options table have names that are constructed as "options_{$field_name}". By using a different value for the post ID you end up with "vacancies_archive_options{$field_name}" and "new_cars_archive_options{$field_name}"

    however, you should try to keep these post ID “names” short because there is a limit on the length of "opion_name" in the DB. I am only using these as examples. I would probably use something simple like "vacancy_options" or "new_car_options".

    Doing this makes it possible to use the same field names because
    if (have_rows('repeater_name', 'vacancy_options'))
    and
    if (have_rows('repeater_name', 'new_car_options'))
    refer to 2 completely different fields even if the repeater field name is the same.

    With that in place you simply need to figure out what “post ID” to use. For this I would create a function that looks at the queried object and then returns the correct ID.

    Then I would use a template part for the "hero_section". This template part would call the function that gets the ID and then loops over the correct repeater using that post ID.

Viewing 2 posts - 1 through 2 (of 2 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.