【WordPress】頻出テンプレートタグと条件分岐タグ
随時更新
テンプレートタグ
名称 | 解説 | 記述例 |
---|---|---|
the_title() | 投稿タイトルを表示 | <a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a> |
esc_html( get_the_title() ) | 投稿タイトルを取得 | |
the_title_attribute() | 属性用投稿タイトルを表示 | |
the_permalink() | 投稿のパーマリンクを表示 | <a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a> |
esc_url( get_permalink() ) | 投稿のパーマリンクを取得 | |
the_time( ‘Y-m-d’ ) | 投稿日時を表示 | <time datetime=”<?php the_time( ‘Y-m-d’ ); ?>”><?php the_time( ‘Y.m.d’ ); ?></time> |
$category = get_the_category(); $category = $category[0]; esc_url( get_category_link( $category->term_id ) ) | 投稿に属するカテゴリーを1つ取得 | <?php $category = get_the_category(); $category = $category[0]; ?> <a href=”<?php echo esc_url( get_category_link( $category->term_id ) ); ?>”><?php echo esc_html( $category->cat_name ); ?></a> |
get_template_directory_uri() | テンプレートディレクトリまでのパス | <img src=”<?php echo esc_url( get_template_directory_uri() ); ?>/assets/images/common/logo.svg” alt=”<?php bloginfo( ‘name’ ); ?>”> |
home_url() | ブログのホームURL | <a href=”<?php echo esc_url( home_url( ‘/’ ) ); ?>”> |
get_template_part( ‘xxx’ ) | テンプレートパーツをテンプレートへ読み込む | get_template_part( ‘template-parts/breadcrumbs’ ) |
the_excerpt() | 抜粋を表示 | if (has_excerpt()) { the_excerpt(); } |
条件分岐タグ
投稿
カスタム投稿タイプ & タクソノミー
is_home()
is_category()
is_singular( 'post' )
is_post_type_archive( '投稿タイプ名' )
is_tax( 'タクソノミー名' )
is_singular( '投稿タイプ名' )