7月, 2011

  1. 検索フォームを拡張するプラグインSearch Everything

    7月 14, 2011 by miccyome

    Search Everything

    WordPress › Search Everything « WordPress Plugins

    Search EverythingはWordpressの検索を拡張するプラグイン
    デフォルトではカスタムフィールドの中身の検索はできない。など
    もっと細かく検索したいときに使う
    (続きを読む…)


  2. [WordPress]親カテゴリ-IDを取得する方法

    7月 8, 2011 by miccyome

    カテゴリ-IDを取得する関数

    テンプレートタグ/get the category

    記事のカテゴリー名、ID、カテゴリースラッグ、説明、親カテゴリーなどの情報をPHPの配列として返す。コード記述例のような形式で変数を使う

    テンプレートタグ/get the category – WordPress Codex 日本語版

    ■親カテゴリ-IDの取得

    表示させているページの親カテゴリ-IDを取得する
    親カテゴリ-に属していない場合は「0」が表示される

    <?php
    // 現在のカテゴリ-を配列取得
    $cat_now = get_the_category();
    // 親の情報を$cat_nowに格納
    $cat_now = $cat_now[0];
    
    //category_parentを$parent_idに格納
    $parent_id = $cat_now->category_parent;
    
    //親カテゴリ-IDを表示
    echo  $parent_id;
    ?>
    

    ■カテゴリ-名、スラッグを取得

    <?php
    // 現在のカテゴリ-を配列取得
    $cat_now = get_the_category();
    // 親の情報を$cat_nowに格納
    $cat_now = $cat_now[0];
    
    //category_name格納
    $cat_name = $cat_now->name;
    //slugを格納
    $slug = $cat_now->name;
    
    //カテゴリ-名を表示
    echo  $cat_name;
    // slugを表示
    echo  $slug;
    
    ?>
    

    ■get_the_category()の返り値

    プロパティ名 データ型 意味
    term_id int ID
    name string 名前
    slug string スラッグ
    term_group int グループID
    term_taxonomy_id int タクソノミーID
    taxonomy string タクソノミー名。カテゴリーの場合は必ず’category’となる
    description string 説明
    parent int 親カテゴリーID。親カテゴリーがない場合は0となる
    count int 投稿数
    cat_ID int カテゴリーID(term_idのエイリアス)
    category_count int 投稿数(countのエイリアス)
    category_description string 説明(descriptionのエイリアス)
    cat_name string カテゴリー名(nameのエイリアス)
    category_nicename string ナイスネーム(slugのエイリアス)
    category_parent int 親カテゴリーID(parentのエイリアス)

    ■get_the_category()の返り値がわからなくなったら

    <?php
    // 現在のカテゴリ-を配列取得
    $cat_now = get_the_category();
    // 親の情報を$cat_nowに格納
    $cat_now = $cat_now[0];
    
    //var_dumpで確認
    echo "<pre>";
    var_dump($cat_now);
    echo "</pre>";
    
    ?>
    

    参照URL

    get_the_category:WordPress私的マニュアル
    親カテゴリーIDの取得 » ITな英語の日々