サポート » 使い方全般 » アクションフックwp_enqueue_style('');の表記はNGでしょうか。

  • 解決済 RyouRyouRyouRyou

    (@ryouryouryouryou)


    【困っていること】
    ○アクションフックを利用し、ページによって読み込むcssを制御しています。
    ○テンプレートがメインで利用しているcssが分からず 、全ページ共通cssの指定を空にしています。
    以下の書き方で動作は問題ないのですが、このままでよろしいでしょうか。

    //* Add new css.
    function register_style() {
    wp_register_style(‘style’, get_bloginfo(‘template_directory’).’/../prose/style.css’);
    wp_register_style(‘home’, get_bloginfo(‘template_directory’).’/../prose/css/home.css’);
    wp_register_style(‘single’, get_bloginfo(‘template_directory’).’/../prose/css/single.css’);
    wp_register_style(‘category’, get_bloginfo(‘template_directory’).’/../prose/css/category.css’);
    }
    function add_stylesheet() {
    register_style();
    // 全ページ共通
    wp_enqueue_style(”);
    // TOPページ専用
    if (is_home()){
    wp_enqueue_style(‘home’);
    }
    // 投稿・カスタム投稿ページ専用
    elseif (is_single()) {
    wp_enqueue_style(‘single’);
    }
    // カテゴリページ専用
    elseif (is_category()) {
    wp_enqueue_style(‘category’);
    }
    }
    add_action(‘wp_print_styles’, ‘add_stylesheet’);

    新規で作成したcssはhome.css、single.css、category.cssです。
    ご教示頂ければ幸いです。よろしくお願い申し上げます。

    【環境】
    アプリケーション名 WordPress 4.3.1
    テンプレート genesis 子テーマProse (http://www.studiopress.com/)

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • wp_enqueue_style の第一引数は有効なスタイルシートのハンドルでないと意味がありません。

    wp_enqueue_style('');

    上記コードは、読む込むスタイルシートがないのであれば記述する必要はないでしょう。

    また、第二引数はスタイルシートのパスではなくURLです。さらに子テーマということなので、

    // wp_register_style('style', get_bloginfo('template_directory').'/../prose/style.css');
    wp_register_style('style', get_stylesheet_directory_uri() . '/style.css');

    が適当です。

    関数リファレンス/wp enqueue style – WordPress Codex 日本語版#パラメータ

    あと、add_stylesheet 関数のフック先が違います。wp_register_style 関数を実行するので

    // add_action('wp_print_styles', 'add_stylesheet');
    add_action('wp_enqueue_scripts', 'add_stylesheet');

    としてください。

    Function Reference/wp register style « WordPress Codex#Usage
    (今見たら、日本語Codexの wp_register_stylewp_enqueue_style の内容になっていますね… >> 関数リファレンス/wp register style – WordPress Codex 日本語版

    なお、有料テーマについては検証ができません。販売元への問合せをお願いすることになりますのでそちらもあらかじめご留意ください。

    トピック投稿者 RyouRyouRyouRyou

    (@ryouryouryouryou)

    とても分かりやすく説明頂き、ありがとうございます!
    無事修正することができました。

    感謝しております。

2件の返信を表示中 - 1 - 2件目 (全2件中)
  • トピック「アクションフックwp_enqueue_style('');の表記はNGでしょうか。」には新たに返信することはできません。