WordPress の テーマ作成の際にループ内で使う項目を、必要な部分だけ抜き出して使えるように、まとめてみました。
if ( have_posts() ) {
|
|
投稿ID |
$post_id = get_the_ID();
|
---|---|
投稿タイプ |
$post_type = get_post_type();
|
投稿タイトル |
$post_title = esc_attr( apply_filters( 'the_title', get_the_title() ) );
|
出力用の整形やプラグインの挙動追加のためにapply_filters( 'the_title', $value ) を使用。プラグインの挙動によりHTML属性値に適さない恐れがあるので esc_attr( $value ) でエスケープ。
|
|
投稿日時 |
$post_date = get_the_time('Y年m月d日H時i分');
|
get_the_date() だと、同一公開日の二つ目以降で取得できないので、get_the_time() を使用。
|
|
更新日時 |
$modified_date = get_the_modified_date('Y年m月d日H時i分');
|
投稿者名(ブログ上の表示名) |
$auther_name = esc_attr( get_the_author() );
|
HTML属性値に適さない恐れがあるのでesc_attr( $value ) でエスケープ。
|
|
投稿者ID |
$auther_id = get_the_author_meta('ID');
|
投稿者のアバター画像出力用HTML |
$auther_avatar = get_avatar( get_the_author_meta('ID'), 96 );
|
96 は、画像サイズ。デフォルトで96 、最大512 。アバター画像を取得できればimg要素の文字列、取得できなければ false を返す。
|
|
投稿者別アーカイブページのURL |
$auther_posts_url = get_author_posts_url( get_the_author_meta('ID') ); $display_auther_posts_url = esc_url( urldecode( $auther_posts_url ) ); $auther_posts_url = esc_url( $auther_posts_url );
|
はじめに取得した値はURLエンティティされていて難読的なため、urldecode( $value ) で可読化。初期値・可読化値の共に、HTMLのhref属性値に適さない恐れがあるので esc_url( $value ) でエスケープ。
|
|
記事のURL |
$permalink = apply_filters( 'the_permalink', get_permalink() ); $display_permalink = esc_url( urldecode( $permalink ) ); $permalink = esc_url( $permalink );
|
出力用の整形やプラグインの挙動追加のためにapply_filters( 'the_permalink', $value ) を使用。はじめに取得した値はURLエンティティされていて難読的なため、 urldecode( $value ) で可読化。可読化値は、HTMLのhref属性値に適さない恐れがあるので esc_url( $value ) でエスケープ。初期値も、プラグインの挙動などによりHTMLのhref属性値に適さない恐れがあるので esc_url( $value ) でエスケープ。
|
|
アイキャッチ画像の情報 |
$thumb_size = 'full'; $thumb_id = get_post_thumbnail_id(); $thumb_obj = wp_get_attachment_image_src( $thumb_id, $thumb_size, true ); $thumb_url = ( $thumb_obj ) ? $thumb_obj[0] : ''; $thumb_width = ( $thumb_obj ) ? $thumb_obj[1] : ''; $thumb_height = ( $thumb_obj ) ? $thumb_obj[2] : '';
|
画像のサイズは、元サイズの'full' と、「管理画面-設定-メディア」で管理されている 'thumbnail' ,'medium' ,'large' のほか、add_image_size() 関数で追加されたサイズ名を使用可能。アイキャッチ画像が設定されているかでの処理の分岐は、 if ( has_post_thumbnail() ) { ... } else { ... } で行える。
|
|
カテゴリー タグ タクソノミー |
$category_terms = get_the_term_list( $post->ID, 'category', '', ',', '' ); $tag_terms = get_the_term_list( $post->ID, 'post_tag', '', ',', '' ); $custom_terms = get_the_term_list( $post->ID, 'custom_taxonomy_name', '', ',', '' );
|
ターム別アーカイブページへのリンク付きターム一覧表示用HTML文字列を取得。 引数:投稿ID, タクソノミー名, 前につく文字列, 間に入れる文字列, 後ろにつける文字列。 例) echo get_the_term_list( $post->ID, 'category', '<ul><li>', ',</li><li>', '</li></ul>' );
|
|
投稿記事の要約文 |
$excerpt = apply_filters( 'the_excerpt', get_the_excerpt() ); $excerpt = strip_tags( $excerpt );
|
出力用の整形やプラグインの挙動追加のためにapply_filters( 'the_excerpt', $value ) を使用。apply_filters( 'the_excerpt', $value ) を使用すると、pタグで囲まれている状態( <p>excerpt text...</p> )になるため、
strip_tags() でHTMLタグを除去してテキストのみ抽出。
|
|
投稿記事の本文 |
$content = apply_filters( 'the_content', get_the_content() ); $content = str_replace( ']]>', ']]>', $content ); ;
|
出力用の整形やプラグインの挙動追加のためにapply_filters( 'the_content', $value ) を使用。CDATAセクション内で値を使用できるよう str_replace( ']]>', ']]>', $value ) で、CDATAブロックの閉じタグを無効化。
|
投稿に関連づけられたタクソノミーとタームを全て表示する場合は、
get_object_taxonomies()
と、get_the_terms()
,get_term_link()
で取得して整形。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
$post_id = get_the_ID(); $post_type = get_post_type(); $taxonomies = get_object_taxonomies( $post_type, 'objects' ); // 関連タクソノミーの情報が格納された配列 $display_taxonomies = ''; // 表示用に整形されたタクソノミー情報を代入する変数 if ( $taxonomies ) { // タクソノミー情報を表示用に整形 $display_taxonomies .= '<div class="post-terms">'."\n"; foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){ $display_taxonomy_slug = $taxonomy_slug; // タクソノミースラッグ $display_taxonomy_name = $taxonomy->label; // タクソノミー名 $terms = get_the_terms( $post_id, $taxonomy_slug ); if ( !empty( $terms ) ) { $display_taxonomies .= '<p class="taxonomy-'.$display_taxonomy_slug.'">'; $display_taxonomies .= $display_taxonomy_name . ":"; foreach ( $terms as $term ) { $term_slug = esc_attr( $term->slug ); // タームのスラッグ $term_link = esc_url( get_term_link( $term_slug, $taxonomy_slug ) ); // ターム別アーカイブページのURL $term_name = esc_attr( $term->name ); // タームの表示名 $display_taxonomies .= '<a class="term-'.$term_slug.'" href="'.$term_link.'" title="'.$term_name.' ArchivePage">'; $display_taxonomies .= $term_name; $display_taxonomies .= '</a>'; } $display_taxonomies .= '</p>'."\n"; } } $display_taxonomies .= '</div><!-- /.post-terms -->'."\n"; echo $display_taxonomies; } |
WebサイトのURL |
$home_url = esc_url( home_url() );
|
---|---|
取得した値の後ろにスラッシュ(/ )はつかない。例)http://www.example.net HTMLのhref属性値に適さない恐れがあるので esc_url( $value ) でエスケープ。
|
|
WordPress本体の インストールディレクトリURL |
$site_url = esc_url( site_url() );
|
取得した値の後ろにスラッシュ(/ )はつかない。例)http://www.example.net/wordpress HTMLのhref属性値に適さない恐れがあるので esc_url( $value ) でエスケープ。
|
|
Webサイトの名前 |
$site_name = esc_attr( get_bloginfo('name') );
|
「管理画面-一般設定」の「サイトのタイトル」で設定された値。 HTML属性値に適さない恐れがあるので esc_attr( $value ) でエスケープ。
|
|
Webサイトのキャッチフレーズ |
$site_description = esc_attr( get_bloginfo('description') );
|
「管理画面-一般設定」の「キャッチフレーズ」で設定された値。 HTML属性値に適さない恐れがあるので esc_attr( $value ) でエスケープ。
|
|
サイト管理者のメールアドレス |
$site_admin_email = get_bloginfo('admin_email');
|
「管理画面-一般設定」の「メールアドレス」で設定された値。 HTML属性値に適さなくなる恐れがあるのでエスケープ処理は行わない。 |
|
テーマファイルのディレクトリURI |
$theme_url = esc_url( get_template_directory_uri() );
|
取得した値の後ろにスラッシュ(/ )はつかない。例)http://www.example.net/wp-content/themes/twentysixteen HTMLのhref属性値に適さない恐れがあるので esc_url( $value ) でエスケープ。子テーマから呼ぶ場合は、親テーマのディレクトリURIを返す。 |
|
子テーマファイルのディレクトリURI |
$child_theme_url = esc_url( get_stylesheet_directory_uri() );
|
取得した値の後ろにスラッシュ(/ )はつかない。例)http://www.example.net/wp-content/themes/customtwentysixteen HTMLのhref属性値に適さない恐れがあるので esc_url( $value ) でエスケープ。有効化された子テーマがある場合は、子テーマのディレクトリURIを返す。 有効化された子テーマがない場合は、有効化されているテーマのディレクトリURIを返す。 |
|
|
|
|
|
ページネーション
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
<?php // 記事関連の整形と出力 if ( have_posts() ) { // 記事が在る時の処理 // 記事下部のナビゲーションを整形 $display_page_nav = ''; // 表示用に整形された前後の記事へのリンク。もしくはページネーション if ( is_page() ) { // 固定ページでは何もいらない } elseif ( is_single() ) { // 投稿のシングルページでは前後の記事のリンク $prev_post_link = ''; // 前の記事のリンクHTML if ( get_previous_post() ) { $prev_post = get_previous_post(); // 前の記事の情報 $prev_post_url = apply_filters( 'the_permalink', get_permalink($prev_post->ID) ); // 前の記事のURL $prev_post_title = esc_attr( apply_filters( 'the_title', get_the_title($prev_post->ID) ) ); // 前の記事のタイトル $prev_post_link .= '<p class="prev-post-link">'; $prev_post_link .= '<a href="'.$prev_post_url.'" title="'.$prev_post_title.'">'; $prev_post_link .= '<span class="prev-post-title">'; $prev_post_link .= $prev_post_title; $prev_post_link .= '</span>'; $prev_post_link .= '</a>'; $prev_post_link .= '</p>'."\n"; } $next_post_link = ''; // 次の記事のリンクHTML if ( get_next_post() ) { $next_post = get_next_post(); // 次の記事の情報 $next_post_url = apply_filters( 'the_permalink', get_permalink($next_post->ID) ); // 次の記事のURL $next_post_title = esc_attr( apply_filters( 'the_title', get_the_title($next_post->ID) ) ); // 次の記事のタイトル $next_post_link .= '<p class="next-post-link">'; $next_post_link .= '<a href="'.$next_post_url.'" title="'.$next_post_title.'">'; $next_post_link .= '<span class="next-post-title">'; $next_post_link .= $next_post_title; $next_post_link .= '</span>'; $next_post_link .= '</a>'; $next_post_link .= '</p>'."\n"; } if ( $prev_post_link || $next_post_link ) { // 前後の記事のリンクを整形 $display_page_nav .= '<div class="page-nav">'."\n"; $display_page_nav .= $prev_post_link; $display_page_nav .= $next_post_link; $display_page_nav .= '</div><!-- /.page-nav -->'."\n"; } } else { // アーカイブページではページネーション // global $wp_query; // 関数化する場合に必要 $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1; // ページ番号 $max_page = $wp_query->max_num_pages; // 総ページ数 $replace_str = 9999999999; $arg = array( 'base' => str_replace( $replace_str, '%#%', esc_url( get_pagenum_link($replace_str) ) ), 'current' => $paged, 'total' => $max_page ); $paginate_links = paginate_links($arg); if ( 1 < $max_page ) { $display_page_nav .= '<div class="pagination">'."\n"; $display_page_nav .= $paginate_links; $display_page_nav .= '</div><!-- /.pagination -->'."\n"; } } // 記事の整形と出力 echo '<div class="entry-posts">'."\n"; while ( have_posts() ) { // 記事ごとにループ処理 the_post(); // ループするごとに記事のプロパティをセット // ここで使う } echo '</div><!-- /.entry-posts -->'."\n"; // ナビゲーションを出力 echo $display_page_nav; } else { // 記事がない時の処理 echo '<div class="undefined-posts"><p>記事がないようです。</p></div>'."\n"; } ?> |