taxonomy_term

タクソノミーのタームに所属する固定ページを全部表示させるのではなく、特定の親ページをもつ子ページだけを表示させたい場合に使えるwordpressコードの覚書です。

絞り込んだ記事のタイトルや概要だけではなく、その記事のカスタムフィールドタームも表示させるようにしています。

[html]
<?php
$get_page_id = get_page_by_path("parent_slug");//固定ページの親名
$get_page_id = $get_page_id->ID;
$my_pages = get_posts(array(
‘post_type’ => ‘page’,
‘posts_per_page’ => ‘-1’,
‘post_parent’ => $get_page_id,
‘orderby’ => ‘menu_order’,
‘order’ => ‘ASC’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘taxonomy_name’, //カスタム分類taxonomy名
‘field’ => ‘slug’,
‘terms’ => $term, // タームtermで絞り込む
)
)
));
?>

<?php if($my_pages):
foreach ($my_pages as $post):
setup_postdata($post);
?>
<!–アイキャッチ画像–>
<?php if(has_post_thumbnail()){ ?>
<div class="left post-thum">
<a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php echo the_post_thumbnail(); ?></a>
</div>
<?php } ?>
<!–/アイキャッチ画像–>
<!–本文抜粋–>

<h3 class="post-title"><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

<p><?php echo esc_html( $post->post_excerpt ); ?></p>//概要表示

▼記事のターム(term)を指定してそのリンク表示させる方法
⇨タームslugを指定
<ul>
<?php
the_terms( $post->ID, ‘fm_slug’, ‘<li>タイプ: ‘, ‘ / ‘, ‘</li>’ );
?>
<?php
the_terms( $post->ID, ‘color_slug’, ‘<li>色: ‘, ‘ / ‘, ‘</li>’ );
?>
<?php
the_terms( $post->ID, ‘size_slug’, ‘<li>サイズ: ‘, ‘ / ‘, ‘</li>’ );
?>
</ul>

▼特定のタームの情報を配列で取得する
⇨taxonomyのslugを指定する
<?php $terms = get_the_terms( $post->ID, ‘taxonomy_slug’ ); ?>
<?php foreach ( $terms as $term ) : ?>
<a href="http://sample.net/<?php echo esc_html( $term->taxonomy ); ?>/<?php echo esc_html( $term->slug ); ?>/"><img src="http://sample.net/<?php echo esc_html( $term->taxonomy ); ?>/<?php echo esc_html( $term->slug ); ?>.png" alt="<?php echo esc_html( $term->name ); ?>"></a>
<?php endforeach; ?>

▼記事のカスタムフィールドを表示させる
⇨カスタムフィールドのkeyを指定
<?php
$key_1_value = get_post_meta( get_the_ID(), ‘sample_key1’, true );
$key_2_value = get_post_meta( get_the_ID(), ‘sample_key2’, true );
// カスタムフィールドが値を持つかチェック
if ( ! empty( $key_1_value ) ) {
echo $key_1_value;
echo $key_2_value;
}
?>
<!–/本文抜粋–>
<?php endforeach;
wp_reset_postdata();
else : ?>
<div class="contents">
<p>お探しの記事は見つかりませんでした。</p>
</div>
<?php endif; ?>
[/html]

参考サイト:
親ページのスラッグを指定して子ページ一覧を表示する