0

I've been browsing some of the other questions and I haven't been able to find a solution. There was a great solution to part of my problem that I'm basing this code on. I need to detect if on mobile, and display the mobile theme with one exception. If a user is on mobile and on page ID 145 the normal theme needs to be used. This is the code I have that works, except does not display the normal theme on that specific page on mobile. I've tried some variations with no luck. Thanks for looking.

add_filter('template', 'change_theme');
add_filter('option_template', 'change_theme');
add_filter('option_stylesheet', 'change_theme');
function change_theme($theme) {
    if ( wp_is_mobile() ) {
        $theme = 'SelectThemeMobile';

    } 

    elseif ( is_page( 145 ) && wp_is_mobile() ) {

        $theme = 'SelectThemeOne';
    }

    return $theme;
}
  • try switching the if and else if. Or if(!is_page(145) && wp_is_mobile()) { // use other template } – pascalvgemert Jul 1 '14 at 23:00
  • Thanks! Those were some of the variations I tried, but didn't work. I am changing directions and will just make a separate template in the mobile theme. This seems like the better approach at this point. – Garrett Jul 2 '14 at 13:44
0

Try:

add_filter('template', 'change_theme');
add_filter('option_template', 'change_theme');
add_filter('option_stylesheet', 'change_theme');
function change_theme($theme) {
    if ( wp_is_mobile() ) {
        $theme = 'SelectThemeMobile';

    } 

    $page = get_page_by_title( 'Title for 145' )

    if ( is_page($page->ID) and wp_is_mobile())
        $theme = 'SelectThemeOne';
        // Replace name below with hook for missing stylesheet
        wp_enqueue_style( 'registered name of missing mobile stylesheet');

    }

    return $theme;
}

I believe you must iterate the entire array of Pages in order to evaluate is_page().

| improve this answer | |
  • Thanks, E Carter. That didn't quite seem to work...It did cause unexpected results on the desired page though... the mobile theme markup to output on page ID 145 but the CSS didn't load. On the other mobile pages it is behaving as expected. Using the proper theme. – Garrett Jul 1 '14 at 17:45
  • @Garrett Does the enqueue_style line help? – eyoung100 Jul 1 '14 at 18:38
  • I will test this, but I realize I poorly described what is being output. On Page 145 the mobile theme markup is still being output... Not the proper markup. – Garrett Jul 1 '14 at 18:44
  • Scrap that code, and tell me if this plugin does the trick – eyoung100 Jul 1 '14 at 19:08
  • Hi E Carter. Thanks for finding that. It didn't seem to do what I needed it to, but this seems like a great plugin.After some more consideration, I will add a separate template for that page within the mobile theme. This seems like a better approach at this point. – Garrett Jul 2 '14 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.