2016-09-19 8 views
0

私はセクションごとにワードプレスのトップダウンWebサイトを作成していますが、どのセクションがページを別のセクションに入れるのが最適かわかりません。 は私も考慮に帯電性能をWordpress - 同じページ内のページを取得する

取りたい

<section id="about"> 
 
    <!-- Content of page --> 
 
</section> 
 
<section id="services"> 
 
    <!-- Content of page --> 
 
</section> 
 
<section id="contacts"> 
 
    <!-- Content of page --> 
 
</section>

感謝の

答えて

2

私はここに簡単な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/

+0

私を助けてくれてありがとう。私のセクションに異なるCSSがある場合は? @MrZiggyStardust – user3242861

+0

section要素の中でpost_class()関数を利用するだけです。 https://codex.wordpress.org/Function_Reference/post_class - 異なって生成されたCSSクラスからすべてのセクションをスタイル付けします。 – MrZiggyStardust

関連する問題