1

我已經創建了自定義的帖子類型,並通過高級自定義字段添加了一些東西。 通常當我不需要從每個類別中選擇我會做這樣的事情。ACF CPT每個類別兩列

<?php if (have_posts()) : while (have_posts()) : the_post(); 
<?php the_field('name_of_field'); ?> 
endwhile; else: ?> 

<?php endif; ?> 

但是現在我必須選擇,如果它是兩個特定類別之一,然後把類別「tjanster」在左欄「produkter」在右欄中我引導網格內。 所以這將是50%'tjanster',50%'produkter'。 我一直在尋找一個瘋狂的解決方案,但只發現了一些函數functions.php的代碼沒有工作,5年的舊帖子,沒有爲我工作。

我所說的是什麼,所以我可以有這樣的一些類似的輸出?

<div class="container"> 
    <div class="row"> 

    <div class="col-sm-6"> 
     <!-- Output of category tjanster. --> 
    </div> 

    <div class="col-sm-6"> 
     <!-- Output of category produkter. --> 
    </div> 

    </div> 
</div> 

回答

0

對於每一列,你需要一個WP_Query()。請參閱代碼以供參考:http://codex.wordpress.org/Class_Reference/WP_Query

<div class="container"> 
    <div class="row"> 
    <div class="col-sm-6"> 

     <?php // tjanster 
     $args1 = array(
     'category_name' => 'tjanster' 
     ); 

     $query1 = new WP_Query($args1);  
     while ($query1->have_posts()) : $query1->the_post(); ?> 
     <h2><?php the_title(); ?></h2> 
     <?php the_content(); ?> 
    <?php endwhile; 
    wp_reset_postdata(); ?> 

    </div> 

    <div class="col-sm-6"> 

     <?php // produkter 
     $args2 = array(
     'category_name' => 'produkter' 
     ); 

     $query2 = new WP_Query($args2);  
     while ($query2->have_posts()) : $query2->the_post(); ?> 
     <h2><?php the_title(); ?></h2> 
     <?php the_content(); ?> 
    <?php endwhile; 
    wp_reset_postdata(); ?> 

    </div> 
</div> 
</div> 
0

我會用query_post像這樣:

<div class="container"> 
    <div class="row"> 
     <div class="col-sm-6"> 
      <h1 class="section_title">tjanster</h1> 
      <?php query_posts(array('category_name' => 'tjanster'); ?> 
      <?php if(have_posts()): while(have_posts()): the_post(); ?> 
       <?php the_field('name_of_field'); ?> 
      <?php endwhile; else: ?> 

      <?php endif; ?> 
     </div> 
     <div class="col-sm-6"> 
      <h1 class="section_title">produkter</h1> 
      <?php query_posts(array('category_name' => 'produkter'); ?> 
      <?php if(have_posts()): while(have_posts()): the_post(); ?> 
       <?php the_field('name_of_field'); ?> 
      <?php endwhile; else: ?> 

      <?php endif; ?> 
     </div> 
    </div> 
</div> 

Source