2016-09-20 33 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

如果您有答案并且我的代码能够正常工作,请将您的问题视为一个完整的问题。 –

相关问题