2階層の固定ページで、親ページのタイトルを取得する
親ページ
|--- 子ページ
|・・・・・・
|--- 子ページ
<?php if(have_posts()):while(have_posts()):the_post() ?>
<?php
$parent_id = $post->post_parent;
$parent = get_page($parent_id)
$parent_title = $parent->post_title;
$echo $parent_title;
?>
<?php endwhile;endif; ?>
親ページと子ページで処理を変える場合の基本コード。
<?php if(have_posts()):while(have_posts()):the_post() ?>
<?php $parent_id = $post->post_parent; ?>
<?php if (!$parent_id) : // 親ページの処理 ?>
<h1><?php the_title(); ?></h1>
<?php else : // 子ページの処理 ?>
<?php
$parent = get_page($parent_id);
$parent_title = $parent->post_title;
?>
<h1><?php echo $parent->post_title ?></h1>
<h2><?php the_title(); ?>
<?php endif; ?>
<?php endwhile;endif; ?>
今後の調査メモ
固定ページはループ内で使用するテンプレートタグ(the_title(), the_content())を使わずに投稿情報$postのプロパティ値のみを使う場合はループ処理は必要ないか?
ループ外とループ内で$postの情報は同じよう。
<?php if(have_posts()):while(have_posts()):the_post() ?>/* 処理 */<?php endwhile;endif; ?>
No comments yet.
改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。