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) ....
Related Posts:
- WooCommerce shop page to use my custom template [closed]
- WordPress get_template_part() function not working
- Dropdown filter by custom field
- How can I optimize this code? [closed]
- ACF Pro make URL Linked in Custom Template [closed]
- Running a function on post update with new post meta
- ACF get_sub_field ALT TAG in the repeater doesn’t show
- wanted to sort the wp_query array by two ACF field (Year and month)
- Custom themes that won’t work when theme is changed
- DIV containing iframe disappears below break point of 992 in Oxygen Builder
- Software for WordPress Theme and Plugin Development? [closed]
- How to detect mobile devices and present them a specific theme?
- Using wp_add_inline_style without a stylesheet
- Does the functions.php file ever get called during an AJAX call? Debug AJAX
- Bestway To Define Theme and Plugin path and url
- enqueue script for specific shortcode
- The difference between calling wp_enqueue_scripts to load scripts and styles in custom theme
- WordPress twentytwenty theme inline css overriding css body
- What is the problem if I use CDN in my WordPress parent theme(built using bootstrap)?
- How can I insert default widgets when my theme is activated (similar to what twenty eleven does)?
- date_i18n() issue
- Active theme responds to theme change request to alert user
- LOST in WP Template Hireachy
- Merging PHP download script into `functions.php`
- Should use widgets in this case?
- Changing the entire control choices using wp.customize with JavaScript
- Static Front Page problem
- Enabling Default Widgets in a Custom Theme
- A theme that follows all the best practices to learn from?
- get_post() function returns post even if it is trashed
- There’s an image named g.gif somewhere in my WordPress site
- WordPress how to override function adjacent_posts_rel_link_wp_head() in link-template.php the correct way
- Can wordpress run in root of existing php site with no theme on index.php but all other WP posts/pages?
- How to regenerate thumbnails when they’re stored on S3
- customizing the_password_form filter
- Whats the safest way to output custom JavaScript and Css code entered by the admin in the Theme Settings?
- WordPress 404 page returning with default in IIS7
- searchform.php override not working
- How to move the the_excerpt’s read more link to another div
- How to create a custom template to admin dashboard
- Change The Meta-Description Of Home-Page?
- Limit Loop to 5 Posts?
- Add, edit specific CPT with custom role
- how to display search term in the template full site editor
- Table of Contents Before first heading
- How to add new page to wordpress theme through coding?
- comment form args are not translatable (customized comment form)
- Zoom on custom theme without child
- Dynamically generated wordpress customizer options?
- Changing a slider to a grid [closed]
- What do with the index.php … [duplicate]
- Code to determine WP version check
- “Theme without sidebar.php is deprecated”
- Is it acceptable for a WordPress theme to not accept widgets?
- get_term_link is Ending to single.php
- making my theme multisite compatible?
- wp e-commerce – theme problem – why won’t it load wpsc-single_product.php? [closed]
- Extending Twenty Eleven Theme
- my excerpts lack the ‘more’ link
- Simple child theme modification but links are broken
- Moving post title down the page / Removing a block from a post
- I want to change background image of my wordpress site on particular post type
- how to retrieve the image title for image Post Format
- Iris color picker in custom theme option page not saving value
- Is functions.php in themes applied to all templates?
- Want to add cart and a search bar in the right of logo line
- how do i develop a global nav and local nav to be wordpress dynamic?
- Why Tags don’t appear in my page?
- First post full width without Sidebar
- Twenty Twelve Author not displaying
- How to make a theme unable to be used on a multisite network?
- Default PHP document / names needed for a WordPress theme?
- How to modify theme content in Thematic?
- How to determine when my theme is deactivated?
- 1 post per category on homepage and counting those posts
- if (has_custom_menu())?
- Exclude parent categories from the_category() within the loop
- Ensure SQL changes carry over
- only the first post is shown
- wordpress can’t find single-project page
- Show Yoast SEO meta tags in Custom WordPress Templates (Theme)
- Is there a way to add classes to a page when using a Gutenberg reusable block as page layout?
- How to show some of category in wordpress
- using themename_the_custom_logo() in template-tags?
- How to make the media library searchable by image title
- WordPress doesn’t load css, image and js files after moving local installation folder
- Custom page template not showing on page creation
- Create settings field on button click
- Undefined index: custom_sidebars
- How To Customise WordPress Comment Submit Button?
- Next/Previous post navigation
- Single Featured Image Repeating
- Main Menu Theme Different Output Print
- How do I move menu to the bottom in Custom Community theme?
- How do I get information about a page, such as featured image, except, and title?
- How can I install my custom-made theme on a wordpress.com blog? [closed]
- IS there any reason not to include javascript in my own post’s embeds?
- How to create more templates to the same theme?
- A problem in loading index.php
- .submenu class missing in output from wp_nav_menu