0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Wordpress ページによって読み込むcssを変える

Last updated at Posted at 2020-03-25

はじめに

wordpressでページごとに読み込むcssを変えたい!と思う事があるかもしれません。

実はphpの条件分岐を使えば、簡単にできるのです。:cop:
今回は固定ページごとに、読み込むcssを変える方法を紹介

スラッグ(Slug)を確認

まずは固定ページのスラッグ(Slug)を確認

スクリーンショット 2020-03-25 18.55.29.jpg

固定ページ一覧>クイック編集から確認できるかと(無理だったら目的のページの編集画面まで行こう)

header.phpで条件分岐

header.php
<head>
.省略
.
.
 <?php if(is_page('movie100')) { ?>
        <link href="<?php echo get_template_directory_uri(); ?>/css/movie100.css" rel="stylesheet ">
        <?php }?>

</head>

これでスラッグがmovie100の固定ページでは、movie100.cssが読み込まれます。

もっと複数の条件分岐をしたい場合

<?php if(is_page(array('movie100', 'manga100'))) : ?>
         <link href="<?php echo get_template_directory_uri(); ?>/css/movie100.css" rel="stylesheet ">
        <?php else: ?>
        <link href="<?php echo get_template_directory_uri(); ?>/css/style.css" rel="stylesheet ">
        <?php endif; ?>

これでスラッグがmovie100manga100の固定ページの場合はmovie100.cssが読み込まれ、それ以外のページではstyle.cssが読み込まれる。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?