Im using a bunch of ACF fields for my WooCommerce email templates and for this example Im calling these in my email_header template. Im using this with WPML, and need to call each language settings. The code works, but seems like it should be a way to write this better.
This is used to set and fetch the correct language based on meta data on orders. Works well. For this I created an array to shorten it down for each if/else statement. NL is the default language.
$language = get_post_meta( get_the_ID(), 'wpml_language', true );
if ($language == 'en') {
For the front, this is used to fetch the right language:
} elseif (ICL_LANGUAGE_CODE=='en') {
For this statement to get the correct language, I need to ad a language prefix to each ACF variable, so it doesn't work to use the variable from the array, it defaults to NL then:
$sitelogo = get_field('en_logo', 'options');
How can I shorten this down? full code:
$header_variabls_one = $sitelogo = get_field('logo', 'options');
$header_variabls_two = $emailLogoLink = get_field('email_logo_link', 'options');
$header_variabls_three = $sitelogo = get_field('logo', 'options');
$header_variabls_four = $header_link_one = get_field('header_links_link_one', 'options');
$header_variabls_five = $header_link_one_url = get_field('header_links_link_one_url', 'options');
$header_variabls_six = $header_link_two = get_field('header_links_link_two', 'options');
$header_variabls_seven = $header_link_two_url = get_field('header_links_link_two_url', 'options');
$header_variabls_eight = $header_link_three = get_field('header_links_link_three', 'options');
$header_variabls_nine = $header_link_three_url = get_field('header_links_link_three_url', 'options');
$header_variabls_ten = $header_link_four = get_field('header_links_link_four', 'options');
$header_variabls_eleven = $header_link_four_url = get_field('header_links_link_four_url', 'options');
$header_variabls_twelve = $ups1 = get_field('header_usps_usp_1', 'options');
$header_variabls_thirteen = $ups2 = get_field('header_usps_usp_2', 'options');
$header_variabls_fourtneen = $ups3 = get_field('header_usps_usp_3', 'options');
$header_variabls = array (
$header_variabls_one,
$header_variabls_two,
$header_variabls_three,
$header_variabls_four,
$header_variabls_five,
$header_variabls_six,
$header_variabls_seven,
$header_variabls_eight,
$header_variabls_nine,
$header_variabls_ten,
$header_variabls_eleven,
$header_variabls_twelwe,
$header_variabls_thirteen,
$header_variabls_fourteen,
);
global $post;
global $sitepress;
$postid = $post->ID;
$language = get_post_meta( get_the_ID(), 'wpml_language', true );
if ($language == 'en') {
add_filter('acf/settings/current_language', function(){return 'en';});
$header_variabls;
} elseif ($language == 'nl') {
add_filter('acf/settings/current_language', function(){return 'nl';});
$header_variabls;
} elseif ($language == 'de') {
add_filter('acf/settings/current_language', function(){return 'de';});
$header_variabls;
} elseif (ICL_LANGUAGE_CODE=='en') {
$sitepress->switch_lang( 'en', true );
do_action( 'wpml_switch_language', "en" );
$sitelogo = get_field('en_logo', 'options');
$emailLogoLink = get_field('en_email_logo_link', 'options');
$sitelogo = get_field('en_logo', 'options');
$header_link_one = get_field('en_header_links_link_one', 'options');
$header_link_one_url = get_field('en_header_links_link_one_url', 'options');
$header_link_two = get_field('en_header_links_link_two', 'options');
$header_link_two_url = get_field('en_header_links_link_two_url', 'options');
$header_link_three = get_field('en_header_links_link_three', 'options');
$header_link_three_url = get_field('en_header_links_link_three_url', 'options');
$header_link_four = get_field('en_header_links_link_four', 'options');
$header_link_four_url = get_field('en_header_links_link_four_url', 'options');
$ups1 = get_field('en_header_usps_usp_1', 'options');
$ups2 = get_field('en_header_usps_usp_2', 'options');
$ups3 = get_field('en_header_usps_usp_3', 'options');
} elseif (ICL_LANGUAGE_CODE=='de') {
$sitelogo = get_field('de_logo', 'options');
$emailLogoLink = get_field('de_email_logo_link', 'options');
$sitelogo = get_field('de_logo', 'options');
$header_link_one = get_field('de_header_links_link_one', 'options');
$header_link_one_url = get_field('de_header_links_link_one_url', 'options');
$header_link_two = get_field('de_header_links_link_two', 'options');
$header_link_two_url = get_field('de_header_links_link_two_url', 'options');
$header_link_three = get_field('de_header_links_link_three', 'options');
$header_link_three_url = get_field('de_header_links_link_three_url', 'options');
$header_link_four = get_field('de_header_links_link_four', 'options');
$header_link_four_url = get_field('de_header_links_link_four_url', 'options');
$ups1 = get_field('de_header_usps_usp_1', 'options');
$ups2 = get_field('de_header_usps_usp_2', 'options');
$ups3 = get_field('de_header_usps_usp_3', 'options');
} elseif (ICL_LANGUAGE_CODE=='nl') {
$sitepress->switch_lang( 'nl', true );
do_action( 'wpml_switch_language', "nl" );
$header_variabls;
}
wordpress
tag to you question. Please take the tour and edit your question. \$\endgroup\$$header_variabls_fourtneen
(on the 14th line) supposed to be$header_variabls_fourteen
(like it is on the 31st line)? \$\endgroup\$