固定ページや投稿記事ごとに個別のCSSとJavaScriptを追加


WordPressの固定ページや投稿記事に個別にcssを追加するWordPressのテーマのスタイルシートとは別に、固定ページや投稿記事に個別にCSSやJavaScriptを指定・追加したい場面があると思います。
とても難しそうな感じがしますが、実は簡単にできるのです。

今回はそんな便利な方法を紹介します。

プラグインは使わないで、テーマ編集の画面でfunction.phpを編集する方法です。

function.phpの編集

function.phpに下記コードを追記します。

CSSの場合


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Custom CSS Widget
add_action('admin_menu', 'custom_css_hooks');
add_action('save_post', 'save_custom_css');
add_action('wp_head','insert_custom_css');
function custom_css_hooks() {
	add_meta_box('custom_css', 'Custom CSS', 'custom_css_input', 'post', 'normal', 'high');
	add_meta_box('custom_css', 'Custom CSS', 'custom_css_input', 'page', 'normal', 'high');
}
function custom_css_input() {
	global $post;
	echo '<input type="hidden" name="custom_css_noncename" id="custom_css_noncename" value="'.wp_create_nonce('custom-css').'" />';
	echo '<textarea name="custom_css" id="custom_css" rows="5" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_custom_css',true).'</textarea>';
}
function save_custom_css($post_id) {
	if (!wp_verify_nonce($_POST['custom_css_noncename'], 'custom-css')) return $post_id;
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
	$custom_css = $_POST['custom_css'];
	update_post_meta($post_id, '_custom_css', $custom_css);
}
function insert_custom_css() {
	if (is_page() || is_single()) {
		if (have_posts()) : while (have_posts()) : the_post();
			echo '<style type="text/css">'.get_post_meta(get_the_ID(), '_custom_css', true).'</style>';
		endwhile; endif;
		rewind_posts();
	}
}

JavaScriptの場合


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Custom JS Widget
add_action('admin_menu', 'custom_js_hooks');
add_action('save_post', 'save_custom_js');
add_action('wp_head','insert_custom_js');
function custom_js_hooks() {
	add_meta_box('custom_js', 'Custom JS', 'custom_js_input', 'post', 'normal', 'high');
	add_meta_box('custom_js', 'Custom JS', 'custom_js_input', 'page', 'normal', 'high');
}
function custom_js_input() {
	global $post;
	echo '<input type="hidden" name="custom_js_noncename" id="custom_js_noncename" value="'.wp_create_nonce('custom-js').'" />';
	echo '<textarea name="custom_js" id="custom_js" rows="5" cols="30" style="width:100%;">'.get_post_meta($post->ID,'_custom_js',true).'</textarea>';
}
function save_custom_js($post_id) {
	if (!wp_verify_nonce($_POST['custom_js_noncename'], 'custom-js')) return $post_id;
	if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
	$custom_js = $_POST['custom_js'];
	update_post_meta($post_id, '_custom_js', $custom_js);
}
function insert_custom_js() {
	if (is_page() || is_single()) {
		if (have_posts()) : while (have_posts()) : the_post();
			echo '<script type="text/javascript">'.get_post_meta(get_the_ID(), '_custom_js', true).'</script>';
		endwhile; endif;
		rewind_posts();
	}
}

両方を指定・追加したい場合には上記2つともfunction.phpに追記します。

ページ・記事作成時に追記する

WordPressにCSSやJavaScriptを個別に追記するfunction.phpの編集後には、固定ページや投稿記事作成画面にこの画像のような入力欄が現れます。
ここに個別に指定・追加したいCSSやJavaScriptを入力します。
すると、固定ページや投稿記事をブラウザが読み込む際にheadタグ内にCSSやJavaScriptが追記されます。
もちろん、ページ・記事作成中のプレビュー画面にも反映されます。

感想・参考

私の場合はCSSを記事ごとに追加したいことが多く、こんな方法をずっと模索していました。
Googleにて検索してみると...
あるじゃないですか!!しかも、プラグインを使わない方法が!!

参考 – WordPress:ページや記事ごとに個別のCSSやJavaScriptを追加する方法

とても助かりました。ありがとうございます!


関連記事

WordPressの画像を連続で投稿に挿入する 連続で画像を投稿に挿入
WordPressにて何枚もの画像を連続で挿入したい時があります。メディアを追加ウィンドウを自動で閉じないカスタマイズを紹介します。
WordPressで関連記事一覧を表示する方法 WordPressで関連記事を一覧表示する
WordPressなどで記事の隅っこによく見かける関連記事の一覧。プラグインを使用せずに実装する方法を紹介します。
WordPressでの子ページの作り方 固定ページを階層化し親ページに子ページ一覧を表示
WordPressの固定ページを階層化し、親ページに子ページの一覧を表示する方法を紹介します。プラグイン不使用です。
アイキャッチ画像の上に半透明の帯と記事タイトルをテキスト表示 画像の上に半透明の帯とテキストを表示
最近よく見かける、画像の上に半透明の帯を表示しテキストを配置する方法。簡単なCSSで実現できますのでお勧めです。
WordPressの記事タイトル一覧にアイキャッチ画像を表示する WordPressの記事タイトル一覧にアイキャッチ画像を表示
WordPressをカスタマイズして、記事タイトル一覧のページを作成している方も多いと思います。各タイトルと一緒にアイキャッチ画像を表示すれば、さらに見やすいページができあがります。

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

*

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>