View category in the preview of Theme Customizer

I am building a theme which Theme customizer changes colors, sizes, etc. and has also the option to choose a category and style that category specifically.

How can I display that category in the preview (only when the user is changing the corresponding options) instead of the default home/front page displayed by default?

Read More

EDIT

I can access the preview window with customize_preview_init, but I can not apply wp_redirect, which I guess could be a way.

Related posts

4 comments

  1. If I understand you goal correctly, then will you a select inside the Customizer, there list the Categories and the User can use this for save in your theme settings.

    If you will enhance the Customizer for categories, then use this class from this repo here. Include the file and enhacne the default class. You can easy implement a select for the categories of the install. Inside the readme of this repo are two links to gist with a example use, but for other class of the repo. I think is clear and the same way on all extensions for the Customizer.

  2. Here’s an approach I used to show things to users while they are viewing the site through the Theme Customizer previewer:

    global $wp_customize;
    if( is_user_logged_in() && isset($wp_customize) ) {
      // Show "categories" or things in the Customizer Previewer only
    }
    

    It makes your templates a bit more messy but, as you’ve probably discovered, there isn’t much documentation/development on Theme customizer :/

  3. I ended up with this solution:

    1.Require an extra file that will contain the hooks only in the theme preview

    function mytheme_customizer_live_preview() {
        require_once('library/preview.php');
    }
    add_action( 'customize_preview_init', 'mytheme_customizer_live_preview' );
    

    2.What I had to do is modify the query as I would normally do in the page, so just do it in the preview (inside preview.php):

    function modify_query($query) {
    
        $category = get_theme_mod( 'mytheme' )['categories'][0];
    
        $query->set('cat', $category);
    
        return $query;
    
    }
    add_filter('pre_get_posts', 'modify_query', 10);
    

Comments are closed.