2016-09-19 4 views
0

저는 섹션별로 wordpress top-down 웹 사이트를 만들고 있는데, 다른 섹션으로 페이지를 가져 오는 가장 좋은 방법은 무엇인지 모르겠습니다. 는 또한 계정에 충전 성능을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

관련 문제