Solarized
HOME > 情報備忘録 > WordPress タグについて
WordPress タグについて
タグの情報を全て取得するのにwp_tag_cloud()使ってたんですがいろいろ余計な属性付きのHTML返ってくるんで調べてたらget_tags()なんてものが。
WordPressってテンプレートタグ
だけでページ作らないとダメなのかと思ってたんですが関数って
のもあるんですね。あんま情報ないですが・・・。
wp_tag_cloud('format=array')の返り値
PHP
- <?php
- array
- 0 => string '<a href='http:
- 1 => string '<a href='http:
- 2 => string '<a href='http:
- 3 => string '<a href='http:
- ・
- ・
- ・
- ?>
get_tags()の返り値
PHP
- <?php
- array
- 0 =>
- object(stdClass)[230]
- public 'term_id' => string '22' (length=2)
- public 'name' => string '3DCG' (length=4)
- public 'slug' => string '3dcg' (length=4)
- public 'term_group' => string '0' (length=1)
- public 'term_taxonomy_id' => string '22' (length=2)
- public 'taxonomy' => string 'post_tag' (length=8)
- public 'description' => string '' (length=0)
- public 'parent' => string '0' (length=1)
- public 'count' => string '3' (length=1)
- 1 =>
- object(stdClass)[227]
- public 'term_id' => string '23' (length=2)
- public 'name' => string '3ds MAX' (length=7)
- public 'slug' => string '3ds-max' (length=7)
- public 'term_group' => string '0' (length=1)
- public 'term_taxonomy_id' => string '23' (length=2)
- public 'taxonomy' => string 'post_tag' (length=8)
- public 'description' => string '' (length=0)
- public 'parent' => string '0' (length=1)
- public 'count' => string '1' (length=1)
- 2 =>
- object(stdClass)[226]
- public 'term_id' => string '24' (length=2)
- public 'name' => string 'BMW' (length=3)
- public 'slug' => string 'bmw' (length=3)
- public 'term_group' => string '0' (length=1)
- public 'term_taxonomy_id' => string '24' (length=2)
- public 'taxonomy' => string 'post_tag' (length=8)
- public 'description' => string '' (length=0)
- public 'parent' => string '0' (length=1)
- public 'count' => string '1' (length=1)
- 3 =>
- object(stdClass)[214]
- public 'term_id' => string '30' (length=2)
- public 'name' => string 'アフィリエイト' (length=21)
- 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)
- public 'term_group' => string '0' (length=1)
- public 'term_taxonomy_id' => string '30' (length=2)
- public 'taxonomy' => string 'post_tag' (length=8)
- public 'description' => string '' (length=0)
- public 'parent' => string '0' (length=1)
- public 'count' => string '6' (length=1)
- ・
- ・
- ・
- ?>
これなら色々と使いやすいですね。
リスト形式のHTMLを作成
PHP
- <?php
- $tag_page = 'http://www.zaku055.com/?tag=';
- $html = '';
-
- foreach (get_tags() as $object) {
-
- $html .= sprintf('<li><a href="%s">%s</a></li>' . "\n", $tag_page . $object->slug, $object->name);
-
- }
-
- $html = sprintf('<ul>%s</ul>', $html);
- var_dump($html);
- ?>
実行結果
PHP
- <?php
- string '<ul><li><a href="http://www.zaku055.com/?tag=3dcg">3DCG</a></li>
- <li><a href="http://www.zaku055.com/?tag=3ds-max">3ds MAX</a></li>
- <li><a href="http://www.zaku055.com/?tag=bmw">BMW</a></li>
- <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>
- ・
- ・
- ・
- ?>
/wp-includes/category.php
get_tags()とget_tag()は/wp-includes/category.phpで定義されてます。
PHP
- <?php
-
- @since
- @see
- @uses
- @param
- @return
- function &get_tags( $args = '' ) {
- $tags = get_terms( 'post_tag', $args );
-
- if ( empty( $tags ) ) {
- $return = array();
- return $return;
- }
-
- $tags = apply_filters( 'get_tags', $tags, $args );
- return $tags;
- }
-
-
-
- @since
- @param
- @param
- @param
- @return
- function &get_tag( $tag, $output = OBJECT, $filter = 'raw' ) {
- return get_term( $tag, 'post_tag', $output, $filter );
- }
- ?>
get_tags()の引数はstring|arrayなんですが何渡しても同じものが返ってきます。
get_terms()がよくわからないのでお手上げです。
get_tag()の方は第一引数にterm_idってのを渡すとそのオブジェクトが返ってきます。
存在しないterm_idの場合nullが返ってきます。
PHP
- <?php
- var_dump(get_tag('20'));
- var_dump(get_tag('1909'));
- ?>
実行結果
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
| 投稿日 |
2009年7月30日 04:29 |
| カテゴリ |
設定 |
| タグ |
WordPress |
| トラックバック URL |
http://www.kantenna.com/cgi-bin/mt504/mt-tb.cgi/1010 |
2009年7月31日 22:03
WordPress カテゴリについてのメモ。get_categories()の返り値メモ続きを読む
2009年10月29日 01:45
Wordpressの便利な機能ショートコードについて続きを読む
2010年1月21日 12:42
WordPressってループ外で何かしようと思うと面倒だなと思ってましたが
$wp_queryとか使うとそれほど面倒じゃなくできそうな感じです。続きを読む
2010年4月27日 23:21
Wordpressのエクスポートとインポート使ってデータ移転するとguidが変わらない続きを読む
2011年9月14日 13:28
WordPressでは、XML-RPCを利用することでエントリーの投稿、取得、カテゴリの作成、編集などをブラウザで管理画面にログインすることなく出来ます。続きを読む
2012年1月14日 05:53
テンプレートのごちゃごちゃが限界にきたのでラッパー書いてみた。でも僕なんかではなくもっとデキる人が書いたものを使いたいw続きを読む