概要説明
このページだけ新規作成したCSSファイルを読み込みたい時に使うカスタマイズ。LPなど他のページでは使わないスタイルを特定のページだけで読み込ませることができます。
~ 目次 ~
トップページにだけ home.css を読み込ませる
functions.php
/*----------------------------------------------------
特定のページにだけ任意のcssファイルを読み込ませる
----------------------------------------------------*/
if ( !function_exists( 'add_page_style' ) ){
function add_page_style() {
// トップページにだけ home.css を読み込ませる
if(is_home()){
wp_enqueue_style( 'page-style-home', get_stylesheet_directory_uri().'/css/home.css' );
}
}
add_action( 'wp_enqueue_scripts', 'add_page_style' );
}
特定の固定ページにpage.cssを読み込ませる場合
functions.php
// 特定の固定ページに page.css を読み込ませる
if(get_the_ID()==56) {
wp_enqueue_style( 'page-style-page', get_stylesheet_directory_uri().'/css/page.css' );
}
特定の記事ページに post.css を読み込ませる
functions.php
// 特定の記事ページに post.css を読み込ませる
global $post;
if($post->ID==81) {
wp_enqueue_style( 'page-style-post', get_stylesheet_directory_uri().'/css/post.css' );
}
複数条件組み合わせてページごとにcssを読み込ませる
functions.php
/*----------------------------------------------------
特定のページにだけ任意のcssファイルを読み込ませる
----------------------------------------------------*/
if ( !function_exists( 'add_page_style' ) ){
function add_page_style() {
// トップページにだけ home.css を読み込ませる
if(is_home()){
wp_enqueue_style( 'page-style-home', get_stylesheet_directory_uri().'/css/home.css' );
}
// 特定の固定ページに page.css を読み込ませる
if(get_the_ID()==56) {
wp_enqueue_style( 'page-style-page', get_stylesheet_directory_uri().'/css/page.css' );
}
// 特定の記事ページに post.css を読み込ませる
global $post;
if($post->ID==81) {
wp_enqueue_style( 'page-style-post', get_stylesheet_directory_uri().'/css/post.css' );
}
}
add_action( 'wp_enqueue_scripts', 'add_page_style' );
}



