2016-09-29 5 views
1

こんにちは私は以下のブロックを簡単にコーディングする方法を探しています。投稿タイプに基づいたカテゴリのif文を実行してから、タイトル/おすすめ画像/コンテンツを含む投稿を吐き出します。 forループでより簡単な方法があるかどうか疑問に思います。WP - コードを簡単に分類できるポストを表示する

は現在、それは次のとおりです。

 if(in_category('hoses-posts')){ 
      $args = array('post_type' => 'hoses_posts' , 'category_name' => 'hoses-posts' , 'order' => 'ASC', 'posts_per_page' => 30); 
      $loop = new WP_Query($args); 
      while ($loop->have_posts()) : $loop->the_post();?> 
      <?php get_template_part('templates/loop-product');?> 
      <?php 
      endwhile; 

     } elseif(in_category('hoses-isobaric')){ 

      $args = array('post_type' => 'hoses_posts' , 'category_name' => 'hoses-isobaric' , 'order' => 'ASC','posts_per_page' => 30); 
      $loop = new WP_Query($args); 
      while ($loop->have_posts()) : $loop->the_post();?> 
      <?php get_template_part('templates/loop-product');?> 
      <?php 
      endwhile; 

     } elseif(in_category('hoses-braid')){ 

      $args = array('post_type' => 'hoses_posts' , 'category_name' => 'hoses-braid' , 'order' => 'ASC','posts_per_page' => 30); 
      $loop = new WP_Query($args); 
      while ($loop->have_posts()) : $loop->the_post();?> 
      <?php get_template_part('templates/loop-product');?> 
      <?php 
      endwhile; 

     } elseif(in_category('hoses-spiral')){ 

      $args = array('post_type' => 'hoses_posts' , 'category_name' => 'hoses-spiral' , 'order' => 'ASC','posts_per_page' => 30); 
      $loop = new WP_Query($args); 
      while ($loop->have_posts()) : $loop->the_post();?> 
      <?php get_template_part('templates/loop-product');?> 
      <?php 
      endwhile; 

     } elseif(in_category('hoses-speciality')){ 
      and so on.... 
     } 

更新:https://gist.github.com/DevinWalker/6fb2783c05b46a2ba251

私はここからのコードのこの部分をつかんでいます。これが正しい方法であるかどうかは不明です。

   $post_type = 'hoses-posts'; 

       // Get all the taxonomies for this post type 
       $taxonomies = get_object_taxonomies(array('post_type' => $post_type));     
       foreach($taxonomies as $taxonomy) :      
        // Gets every "category" (term) in this taxonomy to get the respective posts 
        $terms = get_terms($taxonomy);      
        foreach($terms as $term) : ?> 
        <?php 
         $args = array('post_type' => $post_type, 'order' => 'ASC','posts_per_page' => -1 ,'tax_query' => array(array('taxonomy' => $taxonomy,'field' => 'slug','terms' => $term->slug,))); 
         $posts = new WP_Query($args); 

         if($posts->have_posts()): ?> 

          <?php echo $term->name; ?> 

         <?php while($posts->have_posts()) : $posts->the_post(); ?> 

          <?php get_template_part('templates/loop-product');?>          

         <?php endwhile; endif; ?> 

        <?php endforeach; 

       endforeach; ?> 

答えて

0

)いけない(関数wp_reset_postdataにより近いWP_queryを忘れ

<?php 
function get_template($category_name) { 
    $args = array('post_type' => 'hoses_posts' , 'category_name' => $category_name , 'order' => 'ASC', 'posts_per_page' => 30); 
    $loop = new WP_Query($args); 
    while ($loop->have_posts()) : $loop->the_post(); 
    get_template_part('templates/loop-product'); 
    endwhile; 
} 

if (in_category('hoses-posts')) { 
    get_template('hoses-posts'); 
} elseif (in_category('hoses-isobaric')) { 
    get_template('hoses-isobaric'); 
} elseif (in_category('hoses-braid')) { 
    get_template('hoses-braid'); 
} elseif (in_category('hoses-spiral')) { 
    get_template('hoses-spiral'); 
} elseif (in_category('hoses-speciality')) { 
    and so on.... 
} 
+0

こんにちはTHXをお試しください。残念ながら、私はこれを得ることができません。エラーログに「29-Sep-2016 01:34:53 UTC」と書かれています。PHP致命的なエラー:get_template()を再宣言できません " – roshambo

+0

こんにちは、私は関数名をget_template_hoses(..)に変更しています。おそらく既存のWP機能と衝突するhttps://codex.wordpress.org/Function_Reference/get_template – roshambo

+0

ああちょうどダミー名に似た機能の名前を使用すると、あなたの都合で自由に選ぶことができます。 – Fil

0

私のソリューション

​​

、このようにあなたのコードを最適化する。このため

関連する問題