私はここに簡単なWP_Queryインスタンスを使用して、次のコードを使用します。
<?php
// Set the WP_Query arguments
$args = array(
'post_type' => 'page',
// You need to change this to match your post IDs
'post__in' => array(10, 20, 30),
'order_by' => 'post__in',
'order' => 'DESC' // Might need to change this to "ASC"...
);
// Create the page section query
$page_section_query = new WP_Query($args);
// Simple check...
if($page_section_query->have_posts()) :
while($page_section_query->have_posts()) :
$page_section_query->the_post();
global $post;
// Print out the section!
?>
<section id="<?php echo esc_attr($post->post_name) ?>" <?php post_class(array(esc_attr('page-section-' . $post->post_name))); ?>>
<!-- contents of the page section -->
</section>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
を
シンプルで効果的な、1つのクエリがすべてこれに必要です。より多くのセクションが必要な場合は、さらに投稿IDを追加してください。
投稿IDを注文パラメータとして使用しないようにするには、代わりにmenu_order
を使用します。 post__in
パラメータを使用しない場合は、すべてのページをページの親に追加し、親の投稿IDを使用してそのセクションのすべてのページの子を取得することができます。これは、ソリューションをもう少しモジュール化します。
ここでWP_Queryについて詳しく読む:https://codex.wordpress.org/Class_Reference/
私を助けてくれてありがとう。私のセクションに異なるCSSがある場合は? @MrZiggyStardust – user3242861
section要素の中でpost_class()関数を利用するだけです。 https://codex.wordpress.org/Function_Reference/post_class - 異なって生成されたCSSクラスからすべてのセクションをスタイル付けします。 – MrZiggyStardust