2

I want to change the header-layout in my theme, according to the chosen style on the theme options page with the Redux Framework.

I tried to use a variable in the get_template_part() function, but it doesn't seem to work. After that,I tried to use a switch-statement to change the template used for the header.

$header_layout = $options_demo['header-layout-style'];

switch ($header_layout) {
    case "1":
        get_template_part( 'template-parts/header', '1' ); 
        echo "Template 1";
        break;
    case "2":
        get_template_part( 'template-parts/header', '2' ); 
        break;
    case "3":
        get_template_part( 'template-parts/header', '3' ); 
        break;
    case "4":
        get_template_part( 'template-parts/header', '4' ); 
        break;
    case "5":
        get_template_part( 'template-parts/header', '5' ); 
        break;
    case "6":
        get_template_part( 'template-parts/header', '6' ); 
        break;
    case "7":
        get_template_part( 'template-parts/header', '7' ); 
        break;
    default:
        get_template_part( 'template-parts/header', '1' ); 
}       

When I try the code, I don't see any header layout. I tried to add an echo to see if it outputs some text, and that does work. I checked the paths to the files and they are correct.

Any idea how I can change my template file?

2 Answers2

0

Lead dev of Redux here. More than likely this is a priority issue. Meaning you're loading Redux too late. You have a few options:

  1. Fetch from Redux w/ defaults: Redux::get_options('OPT_NAME', 'KEY')
  2. Fetch from WP, but you won't get defaults: get_option('OPT_NAME')['KEY']
  3. Load redux by doing Redux::init('OPT_NAME')
Dovy
  • 1,210
  • 9
  • 18
0

I figured it out myself by, I made a mistake in the link to search for the right template part

$header_layout = $options_demo['header-layout-style'];
switch ($header_layout) {
    case "1":
        get_template_part( 'template-parts/header/header', '1' ); 
        break;
    case "2":
        get_template_part( 'template-parts/header/header', '2' );
        break;
    case "3":
        get_template_part( 'template-parts/header/header', '3' ); 
        break;
    case "4":
        get_template_part( 'template-parts/header/header', '4' ); 
        break;
    case "5":
        get_template_part( 'template-parts/header/header', '5' ); 
        break;
    case "6":
        get_template_part( 'template-parts/header/header', '6' ); 
        break;
    default:
        get_template_part( 'template-parts/header/header', '1' ); 
}