theme development - Same ACF on two different pages

admin2025-06-06  3

I've made quite an extensive page.php using ACF-repeaters and the flexible content module (1000+ lines of code). The code is getting really spaghetti-like, and I'm not sure how to structure it so it's easier to read and maintain.

Here's a pseudo-code-version of it:

$all_fields = get_fields(); 
foreach( $all_fields as $field ):
  if( $field == 'field_1' ):
    // Deal with field_1
  elseif( $field == 'field_2' ):
    // Deal with field_2
  elseif( $field == 'field_3' ):
    // Deal with field_3
  ...
  ... etc.
  ... etc.
  endif;
endforeach;

Now I'm facing a challenge that will require me to structure the code better; I will need another page-template (page-foobar.php), that includes the same ACF-code as shown above.

On the one hand side, then I thought about making this whole thing into a (or several) functions and then sticking it in functions.php. But then I'm polluting the functions.php-file, which would suck.

Ideally, I would do this 'the WordPress way', if there is such a thing for what I'm doing, but I haven't heard of such a thing. So if there isn't one, - then I'd prefer to have a folder in my theme called 'acf-modules', and then a file for each field, containing a function like this:

function field_1( $field_information ){
  // Deal with $field_information
}

But should I then make an action in functions.php, requiring each file upon WordPress' init-hook? Is that really the cleanest/best way to do this?

I've made quite an extensive page.php using ACF-repeaters and the flexible content module (1000+ lines of code). The code is getting really spaghetti-like, and I'm not sure how to structure it so it's easier to read and maintain.

Here's a pseudo-code-version of it:

$all_fields = get_fields(); 
foreach( $all_fields as $field ):
  if( $field == 'field_1' ):
    // Deal with field_1
  elseif( $field == 'field_2' ):
    // Deal with field_2
  elseif( $field == 'field_3' ):
    // Deal with field_3
  ...
  ... etc.
  ... etc.
  endif;
endforeach;

Now I'm facing a challenge that will require me to structure the code better; I will need another page-template (page-foobar.php), that includes the same ACF-code as shown above.

On the one hand side, then I thought about making this whole thing into a (or several) functions and then sticking it in functions.php. But then I'm polluting the functions.php-file, which would suck.

Ideally, I would do this 'the WordPress way', if there is such a thing for what I'm doing, but I haven't heard of such a thing. So if there isn't one, - then I'd prefer to have a folder in my theme called 'acf-modules', and then a file for each field, containing a function like this:

function field_1( $field_information ){
  // Deal with $field_information
}

But should I then make an action in functions.php, requiring each file upon WordPress' init-hook? Is that really the cleanest/best way to do this?

Share Improve this question edited Nov 3, 2018 at 12:04 cjbj 15k16 gold badges42 silver badges89 bronze badges asked Nov 3, 2018 at 10:50 ZethZeth 9282 gold badges13 silver badges43 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

Well, structuring is partly a matter of taste, but it in this case it seems to be a good idea to include all the functions in a separate file. That includes the code that you would have in both template files. And rather than the chain of elseif you could use PHP's switch statement. So you would have:

In your functions.php (just plain, not depending on a hook)

require_once (get_template_directory() . '/field-functions.php');

In your template files

deal_with_fields ();

In your field-functions.php

function deal_with_fields () {
  $all_fields = get_fields (); 
  foreach ($all_fields as $field) {
    switch ($field) {
      case 'field_1' : deal_with_field_1 ($field); break;
      case 'field_2' : deal_with_field_2 ($field); break;
      ....
      }
    }
  }

function deal_with_field_1 ($field) {
  do your thing;
  }

function deal_with_field_2 ($field) ....    

I just ran into the same problem again. Last time, I solve it using @cjbj's answer. This time I did something smarter, easier to deal with.

The downside of cjbj's answer is, that if you want a rendered file, then the functions deal_with_field_1 and deal_with_field_2 doesn't really work (unless you include something inside them or does a echo file_get_contents(...).

What I did this time, was to have a switch-function in my functions.php and then keep all the sections in seperate files in a folder in the theme.

The switch-function looks like this:

function custom_acf_sections( $acf_sections ){
  foreach( $acf_sections as $section ){
    switch( $section['acf_fc_layout'] ){
      case "section_1":
        include( $_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/THEMENAME/acf-sections/section_1.php' );
        break;
      case "section_2":
        include( $_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/THEMENAME/acf-sections/section_2.php' );
        break;
      case "section_3":
        include( $_SERVER['DOCUMENT_ROOT'] . '/wp-content/themes/THEMENAME/acf-sections/section_3.php' );
        break;
    }
  }
}

It's almost the same as @cjbj's answer, - but I didn't figure it out until I had the same problem for the second time. So I figured I would share my solution.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749145898a316756.html

最新回复(0)