2016-09-20 35 views
0

你好有很好的程序員,我有3個不同的部分在我的網頁。我正在使用wordpress循環來發布wordpress中的所有文章。例如,我的問題是,我想分配其他部分中的每個帖子或某些條件?wordpress發佈一個不同的職位,內部分類的HTML

例如我有3個不同部分 我有一個名爲offer的部分ID,我想顯示僅限於我的部分報價頁面的帖子。

<section id = "offer"> 
       <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <?php if (in_category('3')) : ?> 
     <div class="post-cat-three"> 
    <?php else : ?> 
     <div class="post"> 
    <?php endif; ?> 
      </section> 

下一個部分是段ID命名的項目,我想顯示僅用於我的項目中的部分職位。並把我叫

<section id="projects"> 

     </section> 

最後一節是接觸區域,在我把我的聯繫方式等信息段內,我希望它在接觸部分進行分配。

<section id = "contact"> 

     </section> 

回答

0

爲@Omar Faruque說,你可以這樣做或做一個單一的查詢,然後保存每一部分的內容在變然後做其餘的標記,像這樣:

<?php 

$args = array(

//Category Parameters 
'category__in'  => array(1, 2, 3), //Query only the posts on the categories with IDs 1, 2, 3 

//Type & Status Parameters 
'post_type' => 'post', 

); 

$query = new WP_Query($args); 
$category1_posts = ""; 
$category2_posts = ""; 
$category3_posts = ""; 
if($query->have_post()) { 
    while($query->have_posts()) { 
     $query->the_post(); 
     $thumbnail_url = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID())); 
     if(in_category(1)) { 
      $category1_posts .= '<div class="category1_post">'; 
      $category1_posts .= '<h2>' . get_the_title() . '</h2>': 
      $category1_posts .= '<img src="' . $thumbnail_url . '">'; 
      //Same for other elements you want here, the_excerpt for example 
      $category1_posts .= '</div>'; 
     } else if (in_category(2)) { 
      $category2_posts .= '<div class="category2_post">'; 
      $category2_posts .= '<h2>' . get_the_title() . '</h2>': 
      $category2_posts .= '<img src="' . $thumbnail_url . '">'; 
      //Same for other elements you want here, the_excerpt for example 
      $category2_posts .= '</div>'; 
     } else { 
      $category3_posts .= '<div class="category3_post">'; 
      $category3_posts .= '<h2>' . get_the_title() . '</h2>': 
      $category3_posts .= '<img src="' . $thumbnail_url . '">'; 
      //Same for other elements you want here, the_excerpt for example 
      $category3_posts .= '</div>'; 
     } 
    } 
} 

wp_reset_query(); 
//Then just place the sections and echo the posts you have in your variables 

?> 

<section id="category1"> 
    <?php echo $category1_posts; ?> 
</section> 

<section id="category2"> 
    <?php echo $category2_posts; ?> 
</section> 

<section id="category3"> 
    <?php echo $category3_posts; ?> 
</section> 
0

最好是讓3個不同的查詢3段像下面

<?php 
/** 
* The WordPress Query class. 
* @link http://codex.wordpress.org/Function_Reference/WP_Query 
* 
*/ 
$args = array(

//Category Parameters 
'category__in'  => array(1, 2), 

//Type & Status Parameters 
'post_type' => 'post', 

//Order & Orderby Parameters 
'order'    => 'DESC', 
'orderby'    => 'date', 

//Pagination Parameters 
'posts_per_page'   => 10 

); 

$query = new WP_Query($args); 
if($query->have_post()): 
while($query->have_posts()): $query->the_post(); 
// Wirte your html here 
endwhile; 
endif; 

?> 
+0

謝謝你的回答,先生。它是如何工作的? srry我只是一個初學者 –

+0

哦,對不起,遲到了。您只需在每個部分之前自定義查詢,這是WP的自定義查詢。 –

+0

如果您有答案並且我的代碼能夠正常工作,請將您的問題視爲一個完整的問題。 –

相關問題