2

ユーザーブラウザでワードプレスのテーマを動的に変更したい。ネットでこのコードを見つけて、index.phpファイルに追加しました

add_filter('template', 'switch_theme_by_browser');
add_filter('stylesheet', 'switch_theme_by_browser');

function switch_theme_by_browser($theme) {

    $browser = $_SERVER['HTTP_USER_AGENT'];

    if(preg_match('/MSIE/i',$browser) && !preg_match('/Opera/i',$browser))
    {
        $theme = "twentyeleven";
    }
    elseif(preg_match('/Firefox/i',$browser))
    {
        $theme = "twentyten";
    }
    elseif(preg_match('/Chrome/i',$browser))
    {
        $theme = "Boxoffice";
    }

    return $theme;
}

その後、「致命的なエラー:17行目の/home/xxx/public_html/domain.com/index.phpにある未定義の関数add_filter()を呼び出します」と表示されます。

私が理解しているように、「add_filter()」はワードプレスに組み込まれている関数でなければなりません。

4

2 に答える 2

0

wordpress のルート ディレクトリで、ファイル wp-settings.php のrequire( ABSPATH . WPINC . '/plugin.php' );前に置きます。http://wordpress.org/support/topic/fatal-error-call-to-undefined-function-add_filterrequire( ABSPATH . WPINC . '/functions.php' );でこのソリューションを確認しました。

于 2014-04-12T03:07:30.593 に答える
0

ドーソンが言ったように、これはあなたの/wp-content/themes/[theme]/functions.phpファイルに入れる必要があります。

于 2012-11-07T21:50:47.730 に答える