2016-09-19 61 views
0

我创建一个wordpress自上而下的网站的部分,我不知道哪个是最好的方式来获得页面到不同的部分。 我也想兼顾充电性能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参数,则可以将所有页面添加到页面父页面,并使用父页面标识并获取该部分的所有页面子项。这将使解决方案更加模块化。

查看更多关于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