Help us understand the problem. What is going on with this article?

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

はじめに

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が読み込まれる。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした