ブログ

BLOG

CATEGORY

現在の固定ページに属する子ページの一覧(カスタムフィールド&スラッグあり)

<ul>
<?php
$children= get_children(array(
“post_parent” => get_the_id(), //親ページのIDを取得
‘post_type’ => page,//固定ページ
‘numberposts’ => 10,//表示件数
“post_status” => “publish”//公開中の固定ページに限る
));
$count=0; //連番付与が不要な場合は削除
foreach ( $children as $child ) {
$child_title = $child->post_title; // タイトル
$child_id = $child->ID; // ID
$child_post = get_page( $child_id );
$child_slug = $child_post -> post_name; // スラッグ
$child_permalink = apply_filters( ‘the_permalink’, get_permalink( $child->ID ) );
$count++; //連番付与が不要な場合は削除
?>
<li class=”<?php echo esc_attr($child_slug); ?>”><a href=”<?php echo esc_html($child_permalink); ?>”>
<div class=”txt_wrap cf”>
<div class=”thumbnail heightLine-bx<?php echo esc_attr($count); //連番付与が不要な場合は削除 ?>”></div>
<div class=”text_area heightLine-bx<?php echo esc_attr($count); //連番付与が不要な場合は削除 ?>”>
<h2><?php echo esc_html($child_title); ?></h2>
<p><?php echo esc_html(get_post_custom($child_id)[‘category_txt’][0]); ?></p>
</div>
</div></a>
</li>
<?php } ?>
</ul>


※上記には、ループの回数をcountして連番を振る記述も入っています。(「連番付与が不要な場合は削除」部分)


参考サイト
[Wordpress]固定ページに子ページのカスタムフィールドを一覧表示する
これは便利!WordPressのWP_Queryでよく使うコードスニペット