KANTENNA.COM

Solarized

HOME > 情報備忘録 > WordPress タグについて

WordPress タグについて

タグの情報を全て取得するのにwp_tag_cloud()使ってたんですがいろいろ余計な属性付きのHTML返ってくるんで調べてたらget_tags()なんてものが。

WordPressってテンプレートタグ だけでページ作らないとダメなのかと思ってたんですが関数って のもあるんですね。あんま情報ないですが・・・。

wp_tag_cloud('format=array')の返り値

PHP
  1.  <?php
  2.  array
  3.   0 => string '<a href='http://www.zaku055.com/?tag=%e3%82%a2%e3%83%8a%e3%83%ad%e3%82%b0' class='tag-link-69' title='1 件のトピック' style='font-size: 8pt;'>アナログ</a>' (length=166)
  4.   1 => string '<a href='http://www.zaku055.com/?tag=%e3%82%a2%e3%83%95%e3%82%a3%e3%83%aa%e3%82%a8%e3%82%a4%e3%83%88' class='tag-link-30' title='6 件のトピック' style='font-size: 19.6666666667pt;'>アフィリエイト</a>' (length=214)
  5.   2 => string '<a href='http://www.zaku055.com/?tag=%e3%82%a4%e3%83%a9%e3%82%b9%e3%83%88' class='tag-link-57' title='6 件のトピック' style='font-size: 19.6666666667pt;'>イラスト</a>' (length=178)
  6.   3 => string '<a href='http://ww.zaku055.com/?tag=%e3%82%aa%e3%83%95%e3%82%a3%e3%82%b9' class='tag-link-31' title='1 件のトピック' style='font-size: 8pt;'>オフィス</a>' (length=166)
  7.   ・
  8.   ・
  9.   ・
  10.  ?>

get_tags()の返り値

PHP
  1.  <?php
  2.  array
  3.   0 =>
  4.   object(stdClass)[230]
  5.   public 'term_id' => string '22' (length=2)
  6.   public 'name' => string '3DCG' (length=4)
  7.   public 'slug' => string '3dcg' (length=4)
  8.   public 'term_group' => string '0' (length=1)
  9.   public 'term_taxonomy_id' => string '22' (length=2)
  10.   public 'taxonomy' => string 'post_tag' (length=8)
  11.   public 'description' => string '' (length=0)
  12.   public 'parent' => string '0' (length=1)
  13.   public 'count' => string '3' (length=1)
  14.   1 =>
  15.   object(stdClass)[227]
  16.   public 'term_id' => string '23' (length=2)
  17.   public 'name' => string '3ds MAX' (length=7)
  18.   public 'slug' => string '3ds-max' (length=7)
  19.   public 'term_group' => string '0' (length=1)
  20.   public 'term_taxonomy_id' => string '23' (length=2)
  21.   public 'taxonomy' => string 'post_tag' (length=8)
  22.   public 'description' => string '' (length=0)
  23.   public 'parent' => string '0' (length=1)
  24.   public 'count' => string '1' (length=1)
  25.   2 =>
  26.   object(stdClass)[226]
  27.   public 'term_id' => string '24' (length=2)
  28.   public 'name' => string 'BMW' (length=3)
  29.   public 'slug' => string 'bmw' (length=3)
  30.   public 'term_group' => string '0' (length=1)
  31.   public 'term_taxonomy_id' => string '24' (length=2)
  32.   public 'taxonomy' => string 'post_tag' (length=8)
  33.   public 'description' => string '' (length=0)
  34.   public 'parent' => string '0' (length=1)
  35.   public 'count' => string '1' (length=1)
  36.   3 =>
  37.   object(stdClass)[214]
  38.   public 'term_id' => string '30' (length=2)
  39.   public 'name' => string 'アフィリエイト' (length=21)
  40.   public 'slug' => string '%e3%82%a2%e3%83%95%e3%82%a3%e3%83%aa%e3%82%a8%e3%82%a4%e3%83%88' (length=63)
  41.   public 'term_group' => string '0' (length=1)
  42.   public 'term_taxonomy_id' => string '30' (length=2)
  43.   public 'taxonomy' => string 'post_tag' (length=8)
  44.   public 'description' => string '' (length=0)
  45.   public 'parent' => string '0' (length=1)
  46.   public 'count' => string '6' (length=1)
  47.   ・
  48.   ・
  49.   ・
  50.  ?>

これなら色々と使いやすいですね。

リスト形式のHTMLを作成

PHP
  1.  <?php
  2.  $tag_page = 'http://www.zaku055.com/?tag=';
  3.  $html = '';
  4.  
  5.  foreach (get_tags() as $object) {
  6.  
  7.   $html .= sprintf('<li><a href="%s">%s</a></li>' . "\n", $tag_page . $object->slug, $object->name);
  8.  
  9.  }
  10.  
  11.  $html = sprintf('<ul>%s</ul>', $html);
  12.  var_dump($html);
  13.  ?>

実行結果

PHP
  1.  <?php
  2.  string '<ul><li><a href="http://www.zaku055.com/?tag=3dcg">3DCG</a></li>
  3.  <li><a href="http://www.zaku055.com/?tag=3ds-max">3ds MAX</a></li>
  4.  <li><a href="http://www.zaku055.com/?tag=bmw">BMW</a></li>
  5.  <li><a href="http://www.zaku055.com/?tag=%e3%82%a2%e3%83%95%e3%82%a3%e3%83%aa%e3%82%a8%e3%82%a4%e3%83%88">アフィリエイト</a></li>
  6.  ・
  7.  ・
  8.  ・
  9.  ?>

/wp-includes/category.php

get_tags()とget_tag()は/wp-includes/category.phpで定義されてます。

PHP
  1.  <?php
  2.  /**
  3.   * Retrieves all post tags.
  4.   *
  5.   * @since 2.3.0
  6.   * @see get_terms() For list of arguments to pass.
  7.   * @uses apply_filters() Calls 'get_tags' hook on array of tags and with $args.
  8.   *
  9.   * @param string|array $args Tag arguments to use when retrieving tags.
  10.   * @return array List of tags.
  11.   */
  12.  function &get_tags( $args = '' ) {
  13.   $tags = get_terms( 'post_tag', $args );
  14.  
  15.   if ( empty( $tags ) ) {
  16.   $return = array();
  17.   return $return;
  18.   }
  19.  
  20.   $tags = apply_filters( 'get_tags', $tags, $args );
  21.   return $tags;
  22.  }
  23.  
  24.  
  25.  /**
  26.   * Retrieve post tag by tag ID or tag object.
  27.   *
  28.   * If you pass the $tag parameter an object, which is assumed to be the tag row
  29.   * object retrieved the database. It will cache the tag data.
  30.   *
  31.   * If you pass $tag an integer of the tag ID, then that tag will
  32.   * be retrieved from the database, if it isn't already cached, and pass it back.
  33.   *
  34.   * If you look at get_term(), then both types will be passed through several
  35.   * filters and finally sanitized based on the $filter parameter value.
  36.   *
  37.   * @since 2.3.0
  38.   *
  39.   * @param int|object $tag
  40.   * @param string $output Optional. Constant OBJECT, ARRAY_A, or ARRAY_N
  41.   * @param string $filter Optional. Default is raw or no WordPress defined filter will applied.
  42.   * @return object|array Return type based on $output value.
  43.   */
  44.  function &get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
  45.   return get_term( $tag, 'post_tag', $output, $filter );
  46.  }
  47.  ?>

get_tags()の引数はstring|arrayなんですが何渡しても同じものが返ってきます。 get_terms()がよくわからないのでお手上げです。

get_tag()の方は第一引数にterm_idってのを渡すとそのオブジェクトが返ってきます。 存在しないterm_idの場合nullが返ってきます。

PHP
  1.  <?php
  2.  var_dump(get_tag('20'));
  3.  var_dump(get_tag('1909'));
  4.  ?>

実行結果

object(stdClass)[283]
  public 'term_id' => string '20' (length=2)
  public 'name' => string '素材' (length=6)
  public 'slug' => string '%e7%b4%a0%e6%9d%90' (length=18)
  public 'term_group' => string '0' (length=1)
  public 'term_taxonomy_id' => string '54' (length=2)
  public 'taxonomy' => string 'post_tag' (length=8)
  public 'description' => string '' (length=0)
  public 'parent' => string '0' (length=1)
  public 'count' => string '7' (length=1)
  public 'object_id' => string '376' (length=3)
null
WordPress ポケットリファレンス (POCKET REFERENCE)
作者:村上 晴美 | 価格:¥ 2,604

トラックバック(6)

2009年7月31日 22:03

WordPress カテゴリについてのメモ情報備忘録

WordPress カテゴリについてのメモ。get_categories()の返り値メモ続きを読む

2009年10月29日 01:45

Wordpress ショートコードWEBプログラム覚書

Wordpressの便利な機能ショートコードについて続きを読む

2010年1月21日 12:42

WordPressのカテゴリ判定についてWEBプログラム覚書

WordPressってループ外で何かしようと思うと面倒だなと思ってましたが $wp_queryとか使うとそれほど面倒じゃなくできそうな感じです。続きを読む

2010年4月27日 23:21

Wordpressのエクスポートとインポート使ってデータ移転するとguidが変わらないWEBプログラム覚書

Wordpressのエクスポートとインポート使ってデータ移転するとguidが変わらない続きを読む

2011年9月14日 13:28

[WordPress]XML-RPCを利用して外部から投稿したりする。WEBプログラム覚書

WordPressでは、XML-RPCを利用することでエントリーの投稿、取得、カテゴリの作成、編集などをブラウザで管理画面にログインすることなく出来ます。続きを読む

2012年1月14日 05:53

[WordPress] テンプレートのごちゃごちゃが限界にきたのでラッパー書いてみた。WEBプログラム覚書

テンプレートのごちゃごちゃが限界にきたのでラッパー書いてみた。でも僕なんかではなくもっとデキる人が書いたものを使いたいw続きを読む

コメント

コメントする
Name
Email Address
URL
TIME LINE
2012
02.10

[雑記]縦横がランダムな要素を隙間なく並べるのは無理なのか。

サイトマップ作ったんだけど、やりたかったことが出来なかった。

2012
02.08

[Ubuntu11.10]Winows XPユーザーがUbuntuを使ってみて

Windows XPとUbuntuのデュアルブート環境にしてみておもったこと。

2012
02.03

[jQuery]closest()とparents()

今から16年前Netscapeブラウザのソースコードの公開方法の会議の中で、初めてオープンソースという言葉が使われた今日、 みなさんいかがお過ごしでしょうか。

今回の投稿はそれとは関係のないjQueryのparents()とclosest()が 似てたので何が違うのか試した時のメモです。

2012
02.01

[jQuery Mobile]初期化イベントメモ

jQuery Mobileは読み込まれてから

mobileinit -> pagebeforecreate -> pagecreate -> pageinit

の順番でイベントが発生する。

2012
02.01

[WordPress]管理画面のフッタのフィルタとアクション。

時間も時間なので、あまり使用頻度の高くない微妙なネタを。

会員制サイトなどで不特定多数の人にログインさせる場合、フッタのバージョン情報は隠しておきたかったり、 こじゃれたメッセージを表示したいときとか使うアクションとフィルタ。

2012
01.28

[WordPress] ブログの情報を取得する方法とノーキャッシュ疑惑

ブログのget_bloginfo()で取れないデータが必要な場合に使うもの。

2012
01.26

[jQuery Mobile]ダイアログ

jQuery Mobileにおけるダイアログの扱いはウインドウではなくページ。 なので通常のページと同様に扱える。ダイアログウインドウを出す。じゃなくダイアログページに遷移する。的な。

2012
01.25

[Ubuntu11.10]Ubuntuのインストール後の設定

インストール完了後の環境構築。PC起動時いちいちBIOSからブートドライブ選んで ブートするのかと思ってたら起動時にOS選択画面が出てくる。

すばらしいですね。

2012
01.24