2016-09-20 20 views
0

こんにちは、良いプログラマー、私は私のWebページに3つのセクションがあります。私はワードプレスですべての投稿を投稿するためにワードプレスループを使用しています。私の問題は、私は他のセクションや何かの条件のすべての投稿を割り当てたいですか?wordpress他の投稿をhtmlのカテゴリー内に投稿する

たとえば、私は3つの異なるセクションを持っています 私はセクション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> 

次のセクションはprojectという名前のセクションです。私は自分のプロジェクトセクションのみを対象とした投稿を表示したいと思います。そして

<section id="projects"> 

     </section> 

という名前の私のセクションの内側に入れ、最後のセクションでは、私は私の連絡先の詳細およびその他の情報を入れて接触部である、と私はそれが接点部に割り当てることにしたいです。

<section id = "contact"> 

     </section> 

答えて

0

以下のように3セクションの3別のクエリを作るです次のようにマークアップの残りの部分を行います。

<?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

ベストあなたは、変数上のすべてのセクションの内容を保存し、その後、@Omar Faruqueが言うようにそれを行うか、1つのクエリを行うことができます

<?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

あなたは答えが得られれば私のコードが働いていればあなたの質問を完全にしてください。 –

関連する問題