0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【WordPress】自分で作成したCSSをサイトの全ページに反映させる

Last updated at Posted at 2025-09-28

経緯

見出しに対して、下線を引くクラスを設定したく、共通のCSSを定義できたらいいなと思ったのが発端です。
(画像の赤いところに特定のクラスを設定したら、下線を引けるようにしたい)
image.png

やり方

1.CSSクラスを設定

ブロックを選択し、追加CSSクラスを設定。
今回の場合はhead-lineと設定
image.png

2.フォルダ構成を作成

任意のフォルダ名で、フォルダ中に下記のファイルを作成する。

フォルダ構成
任意のフォルダ名
 ├ functions.php
 └ style.css

自分の場合は、mycssというフォルダにfunctions.phpstyle.cssを作成
image.png

3.function.phpを編集

下記のコードを入れる。
※どんなCSSを設定する場合も固定で大丈夫です。

function.php
<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
  wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
  wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() . '/style.css', array('parent-style')
);
}
?>

4.style.cssを編集

下記のコードを入れる
(ここで自分が反映させたいCSSを設定)

style.css
/*
Theme Name: [フォルダ名] (例:mycss)
Version: [任意のバージョン (例:1)]
Template: [自分が使用している親テーマ (例:Twenty Twenty-Five)]
*/
.head-line{
	border-bottom: 1px solid #cdcdcd
}

※親テーマは自分が今使用しているWordPressのデザインです。
管理ページ → 外観 → テーマを参照してください。
image.png

5.フォルダを圧縮

image.png
圧縮後
image.png

6.圧縮したファイルを読み込み

管理ページ → 外観 → テーマからテーマの追加ボタンを押下
image.png
テーマのアップロードを押下
image.png
ファイルを選択し、今すぐインストールを押下
image.png

下記のようにテーマのインストールが完了しました。と出たら完了
image.png

7.テーマを有効化

管理ページ → 外観 → テーマから有効化ボタンを押下する。
image.png

7.CSSが反映されていることを確認

image.png

CSSを編集したい場合

管理ページ → 外観 → テーマファイルエディターから簡単に編集できます。
image.png
試しに10pxに変更してみた。
※更新したら下のほうにファイルを更新ボタンがあるので押すのを忘れずに
image.png
ちゃんと反映される。
image.png

締め

無事設定できました!
少し面倒ですが、毎回すべてのブロックにCSSを適用するよりは楽だと思うので、やり得ですね!

以上。お疲れさまでした。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?