WordPress のリンク出力タグでは説明は出力されません。
1 | <?php wp_list_bookmarks(); ?> |
説明はリンクタグの title 属性に含まれて、マウスカーソルを合わせると説明が出力されるという仕様ですが、これを以下のように説明もきちんと出力して、一種のページとして成り立たせたい時があります。
1 2 3 4 5 6 7 8 | <h2><a href="http://codex.wordpress.org/" target="_blank">Documentation</a></h2> <p>このリンクはとても凄いリンクです。参考にしましょう。</p> <h2><a href="http://codex.wordpress.org/" target="_blank">Documentation</a></h2> <p>このリンクはとても凄いリンクです。参考にしましょう。</p> <h2><a href="http://codex.wordpress.org/" target="_blank">Documentation</a></h2> <p>このリンクはとても凄いリンクです。参考にしましょう。</p> |
これを実現する方法を紹介します。
リンク一覧で説明も出力する方法
まず以下のように出力するリンクを昨日紹介した方法で PHP の値として受け取ります。
1 | $output = wp_list_bookmarks(Array('echo' => false)); |
そして、先日書いたPHP Simple HTML DOM Parser の使い方を参考にライブラリをダウンロードしてきて、以下のような関数を組み上げます。
1 2 3 4 5 6 7 8 9 10 | function custom_bookmarks() { include('simplehtmldom_1_5/simple_html_dom.php'); $output = wp_list_bookmarks(Array('echo' => false)); $data = str_get_html($output); foreach($data->find('a') as $element) { echo sprintf('<h2><a href="%s" target="_blank">%s</a></h2>', $element->href, $element->plaintext); echo sprintf('<p>%s</p>', $element->title); } } |
コードを書き終わったら、functions.php に追加して、テーマファイルの好きな場所で以下のタグを書きます。
1 | <?php custom_bookmarks(); ?> |
すると以下のように出力されます。
1 2 3 4 5 6 7 8 | <h2><a href="http://codex.wordpress.org/" target="_blank">Documentation</a></h2> <p>このリンクはとても凄いリンクです。参考にしましょう。</p> <h2><a href="http://codex.wordpress.org/" target="_blank">Documentation</a></h2> <p>このリンクはとても凄いリンクです。参考にしましょう。</p> <h2><a href="http://codex.wordpress.org/" target="_blank">Documentation</a></h2> <p>このリンクはとても凄いリンクです。参考にしましょう。</p> |
リンク集のページを作るときとかに使えますので参考にしてみてください。応用すればいろんな形で出力することができます。
コメント