WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Native Imaging
    Member
    Posted 10 months 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
    Member
    Posted 10 months 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
        $wp_customize->add_setting(
    	 'masthead_layout', array(
            'default'        => 'masthead-full.php',
    		'transport'   => 'refresh',
        ) );
    
        $wp_customize->add_control(
    	 '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.

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

Reply

You must log in to post.

About this Topic

Tags

No tags yet.