LoginSignup
1
4

More than 5 years have passed since last update.

WordPress 記事内でpcとスマホの表示内容を切り替えたい。

Posted at

テンプレート内だったらwp_is_mobile()を使えばOK。
投稿内はどうすればいいのか調べた。
ショートコードを使う!

1 function.phpに以下記述 ショートコード登録

function.php
//PCでのみ表示するコンテンツ
function if_is_pc($atts, $content = null ){
$content = do_shortcode( $content);
    if(!wp_is_mobile()){
        return $content;
    }
}
add_shortcode('pc', 'if_is_pc');

//スマートフォンで表示するコンテンツ
/*タブレットも含まれる*/
function if_is_sp($atts, $content = null ){
$content = do_shortcode( $content);
    if(wp_is_mobile()){
        return $content;
    }
}
add_shortcode('sp', 'if_is_sp');

2 使い方

pcで表示させたいコンテンツ

[pc]

pcだよー


[/pc]

spで表示させたいコンテンツ

[sp]

スマホだよー


[/sp]

終わり

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4