The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

need help to use get_theme_mod for including a php file (2 posts)

  1. Native Imaging
    Posted 2 years ago #

    Hello, I'm working on extending the WP Theme Customizer using 'get_theme_mod'.

    Previously, I was using a lot of if statements to parse structure of elements with get_theme_mod options, typically in my header.php file to have different layouts and classes for the header.

    What I'm trying to do now is actually simplify it a little bit by using a php include for different header.php files for easier structuring of different layouts. I have a sub folder called "masthead-layouts" and files like "masthead-full.php" or "masthead-small.php". I figured this would be a cleaner way to have different header structures rather than just using CSS & classes to organize elements and their positions.

    Here's ideally what I would like to happen, but i'm not the best at parsing php.

    <?php include("header-layouts/<?php echo get_theme_mod( 'page_font_script'); ?>"); ?>

    I know this isn't formatted properly, but you should get the idea of what I'm trying to accomplish.
    In my theme options, I have a get_theme_mod with radio options for "header-full.php" and "header-small.php". This option is placed in the header.php file, then should grab the structure of the masthead layout that is desired.

    Maybe including masthead.php files aren't really the best way, but just trying to keep it easier to make variations in the future by making new masthead.php layouts.

  2. Native Imaging
    Posted 2 years ago #

    Aha, got it.
    incase anyone else wants to use the Native WP Theme Customizer, here's my snippets.

    This is my setting and controller with custom array that cues in my masthead.php layouts. This could be used for any type of theme files you would want to use for customizing structure options without having to rely on extensive CSS and class selectors.

    //Header Layout
    	 'masthead_layout', array(
            'default'        => 'masthead-full.php',
    		'transport'   => 'refresh',
        ) );
    	 'masthead_layout', array(
            'label'   => 'Header Layout',
            'section' => 'theme_design_options',
            'type'    => 'radio',
            'settings'    => 'masthead_layout',
    		'priority' => 1,
    		'orderby' => 'menu-order',
    		'choices'    => array(
    			'masthead-small.php' => 'Small',
    			'masthead-full.php' => 'Large',
        ) );

    Hook in header.php to grab the masthead-layout.php file i want to show.

    $tmp = get_theme_mod( 'masthead_layout', 'masthead-full.php' );
    include("masthead-layouts/" . $tmp ); 

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.