Search code examples
phpwordpressredux-framework

How can I switch my template with get_template_part() according to my value in my Redux Framework options page?


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?


Solution

  • 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' ); 
    }