WordPressのテンプレートファイル上などで、様々な値を取得・表示する方法をまとめました。
header.php、footer.phpなどのパーツテンプレートから、$post, $wp_queryなどの変数にアクセスする場合、
のようにグローバル宣言をする必要があります。
投稿関連
|
値取得 |
echo |
|
| シングルページ・固定ページ内 |
| タイトル |
single_post_title('', false) |
single_post_title() |
|
| ID |
get_queried_object_id() |
|
|
| 投稿オブジェクト |
get_queried_object() |
|
|
| ループ内 |
| タイトル |
get_the_title() |
the_title() |
関連: the_title_attribute |
| 内容 |
get_the_content() |
the_content() |
関連: get_extended |
| 抜粋 |
get_the_excerpt() |
the_excerpt() |
|
| パーマリンク |
get_permalink() |
the_permalink() |
関連: get_page_link |
| ID |
get_the_ID() |
the_ID() |
$id、$post->IDでも取得可能 |
| スラッグ |
$post->post_name |
|
|
| 投稿タイプ |
get_post_type() |
|
|
| ループインデックス |
$wp_query->current_post |
|
0スタート・メインループのみ |
| 次の投稿へのリンク |
get_next_post_link() |
next_post_link() |
|
| 前の投稿へのリンク |
get_previous_post_link() |
previous_post_link() |
|
| 投稿IDから取得 |
| タイトル |
get_the_title($id) |
|
|
| 内容 |
get_post_field('post_content', $id); |
|
|
| 抜粋 |
get_post_field('excerpt', $id); |
|
|
| パーマリンク |
get_permalink($id) |
|
|
| スラッグ |
get_post_field('post_name', $id); |
|
|
| 投稿タイプ |
get_post_type($id) |
|
|
ページング関連
|
値取得 |
echo |
|
| 現在のページ |
$paged |
|
1ページ目は0か1 |
| 全ページ数 |
$wp_query->max_num_pages |
|
|
| ページ毎の表示件数 |
get_query_var('posts_per_page') |
|
|
| 現在のページの件数 |
$wp_query->post_count |
|
|
| 全件数 |
$wp_query->found_posts |
|
|
| 次のページへのリンク |
get_next_posts_link() |
next_posts_link() |
|
| 前のページへのリンク |
get_previous_posts_link() |
previous_posts_link() |
|
| 前後のページへのリンク |
get_posts_nav_link() |
posts_nav_link() |
|
日付関連
|
値取得 |
echo |
|
| 年アーカイブURL |
get_year_link($year) |
|
アーカイブページ、ループ内では、引数を''にすると、そのページ/記事の年, 月, 日になる |
| 月アーカイブURL |
get_month_link($year, $month) |
|
| 日アーカイブURL |
get_day_link($year, $month, $day) |
|
| 日付アーカイブページ内 |
| 年月日タイトル |
single_month_title('', false) |
single_month_title() |
日本語の場合、表示に問題あり
記事: 日付アーカイブページのタイトルを修正 |
| 年 |
get_query_var('year') |
|
|
| 月 |
get_query_var('monthnum') |
|
|
| 日 |
get_query_var('day') |
|
|
| ループ内 |
| 投稿日時 |
get_the_time($d) |
the_time($d) |
関連: get_the_date, get_post_time |
| 更新日時 |
get_the_modified_time($d) |
the_modified_time($d) |
関連: get_the_modified_date,
get_post_modified_time |
$dは日付・時刻のフォーマット。$dをget_option('date_format')にすると、ダッシュボードで設定した日付のフォーマットを使用する。 |
| 投稿IDから取得 |
| 投稿日時 |
get_the_time($d, $id) |
|
|
| 更新日時 |
get_post_modified_time($d, null, $id, true) |
|
|
投稿者関連
|
値取得 |
echo |
| 投稿者名 |
get_userdata($author_id)->display_name |
|
| 投稿者アーカイブURL |
get_author_posts_url($author_id) |
|
| 投稿者アーカイブページ内 |
| 投稿者表示名 |
get_queried_object()->data->display_name |
|
| 投稿者ID |
get_queried_object_id() |
|
| 投稿者名 |
get_queried_object()->data->user_nicename |
|
| ループ内 |
| 投稿者表示名 |
get_the_author() |
the_author() |
| 投稿者ID |
get_the_author_meta('ID') |
the_author_meta('ID') |
| 投稿者アーカイブリンク |
|
the_author_posts_link() |
| 投稿IDより取得 |
| 投稿者ID |
get_post_field('post_author', $id) |
|
カテゴリ・タグ・タクソノミー関連
カテゴリ
|
値取得 |
echo |
|
| カテゴリID |
get_cat_ID($cat_name) |
|
スラッグではなく名前 |
| カテゴリオブジェクト |
get_category($category) |
|
$categoryはカテゴリIDかオブジェクト |
get_category_by_slug($slug) |
|
|
get_term_by($field, $value, 'category') |
|
|
| カテゴリ名 |
get_cat_name($cat_id) |
|
|
| カテゴリアーカイブURL |
get_category_link($cat_id) |
|
|
| カテゴリアーカイブページ内 |
| カテゴリ名 |
single_cat_title('', false) |
single_cat_title() |
|
| カテゴリID |
get_queried_object_id() |
|
|
| カテゴリオブジェクト |
get_queried_object() |
|
|
| ループ内 |
| カテゴリリンク |
get_the_category_list() |
the_category() |
|
| カテゴリオブジェクト |
get_the_category() |
|
|
| 投稿IDから取得 |
| カテゴリリンク |
get_the_category_list('', '', $id) |
the_category('', '', $id) |
|
| カテゴリオブジェクト |
get_the_category($id) |
|
|
タグ
|
値取得 |
echo |
|
| タグオブジェクト |
get_tag($tag) |
|
$tagはタグIDかオブジェクト |
get_term_by($field, $value, 'post_tag') |
|
|
| タグアーカイブURL |
get_tag_link($tag_id) |
|
|
| タグアーカイブページ内 |
| タグ名 |
single_tag_title('', false) |
single_tag_title() |
|
| タグID |
get_queried_object_id() |
|
|
| タグオブジェクト |
get_queried_object() |
|
|
| ループ内 |
| タグリンク |
get_the_tag_list() |
the_tags() |
|
| タグオブジェクト |
get_the_tags() |
|
|
| 投稿IDから取得 |
| タグリンク |
get_the_tag_list('', '', '', $id) |
|
|
| タグオブジェクト |
get_the_tags($id) |
|
|
タクソノミー・ターム
|
値取得 |
echo |
|
| タームオブジェクト |
get_term($term, $taxonomy) |
|
$termはタームIDかオブジェクト |
get_term_by($field, $value, $taxonomy) |
|
|
| タームアーカイブURL |
get_term_link($term, $taxonomy) |
|
$termはタームIDかオブジェクト。オブジェクトの場合、$taxonomyは省略可 |
| タクソノミーアーカイブページ内 |
| ターム名 |
single_term_title('', false) |
single_term_title() |
|
| タームID |
get_queried_object_id() |
|
|
| タームオブジェクト |
get_queried_object() |
|
|
| ループ内 |
| タームリンク |
get_the_term_list(0, $taxonomy) |
the_terms(0, $taxonomy) |
|
| タームオブジェクト |
get_the_terms(0, $taxonomy) |
|
|
| 投稿IDから取得 |
| タームリンク |
get_the_term_list($id, $taxonomy) |
the_terms($id, $taxonomy) |
|
| タームオブジェクト |
get_the_terms($id, $taxonomy) |
|
|