Same ACF on two different pages
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
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?
theme-development advanced-custom-fields
add a comment |Â
up vote
1
down vote
favorite
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?
theme-development advanced-custom-fields
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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?
theme-development advanced-custom-fields
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?
theme-development advanced-custom-fields
theme-development advanced-custom-fields
edited 2 hours ago
cjbj
10.7k102866
10.7k102866
asked 3 hours ago
Zeth
13211
13211
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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) ....
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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) ....
add a comment |Â
up vote
2
down vote
accepted
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) ....
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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) ....
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) ....
answered 2 hours ago
cjbj
10.7k102866
10.7k102866
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f318318%2fsame-acf-on-two-different-pages%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password