2017-07-26 40 views
0

我正在尋找一種方法來注入一個類到一個自定義帖子類型的基礎上關閉帖子歸因於類別的div。將有三個類別;添加類到div包裹自定義帖子類型基於關閉帖子類別

電子書(類名CAT1
信息圖表(類名CAT2
案例研究(類名CAT3

這裏是我的代碼,到目前爲止,調用自定義帖子類型。目前,該頁面將所有帖子顯示爲最後一個類別 - 案例研究(cat3)。

 <?php $loop = new WP_Query(array('post_type' => 'resources', 'posts_per_page' => -1)); ?> 
     <?php while ($loop->have_posts()) : $loop->the_post(); ?> 

      <?php 
      $post = $wp_query->post; 

      if (in_category('ebook', $post->ID)) { ?> 
       <div <?php body_class('tile scale-anm cat1 all end'); ?>> 
        <section class="small-12 medium-4 large-3 columns end"> 
         <section class="grid"> 
          <figure class="effect-sarah"> 
           <img src="<?php the_field('img'); ?>" alt="img13"/> 
           <figcaption> 
            <h2>Resource</h2> 
            <p class="signika"><?php the_field('desc'); ?></p> 
            <p class="bold"><?php the_field('btnText'); ?></p> 
            <a href="<?php echo esc_url(get_permalink()); ?>" target="_blank"><?php the_field('btnText'); ?></a> 
           </figcaption>   
          </figure> 
         </section> 
        </section> 
       </div> 
      <?php 
      } 


      elseif (in_category('infographic', $post->ID)) { ?> 
       <div <?php body_class('tile scale-anm cat2 all end'); ?>> 
        <section class="small-12 medium-4 large-3 columns end"> 
         <section class="grid"> 
          <figure class="effect-sarah"> 
           <img src="<?php the_field('img'); ?>" alt="img13"/> 
           <figcaption> 
            <h2>Resource</h2> 
            <p class="signika"><?php the_field('desc'); ?></p> 
            <p class="bold"><?php the_field('btnText'); ?></p> 
            <a href="<?php echo esc_url(get_permalink()); ?>" target="_blank"><?php the_field('btnText'); ?></a> 
           </figcaption>   
          </figure> 
         </section> 
        </section> 
       </div> 
      <?php 
      } 


      elseif (in_category('casestudy', $post->ID)) { ?> 
       <div <?php body_class('tile scale-anm cat3 all end'); ?>> 
        <section class="small-12 medium-4 large-3 columns end"> 
         <section class="grid"> 
          <figure class="effect-sarah"> 
           <img src="<?php the_field('img'); ?>" alt="img13"/> 
           <figcaption> 
            <h2>Resource</h2> 
            <p class="signika"><?php the_field('desc'); ?></p> 
            <p class="bold"><?php the_field('btnText'); ?></p> 
            <a href="<?php echo esc_url(get_permalink()); ?>" target="_blank"><?php the_field('btnText'); ?></a> 
           </figcaption>   
          </figure> 
         </section> 
        </section> 
       </div> 
      <?php 
      } 

      ?>   

     <?php endwhile; wp_reset_query(); ?> 

回答

0

幹,不要重複自己。你的循環也有一些問題。

使用WP_Query後,請使用wp_reset_postdata()而不是wp_reset_query()

擺脫$post = $wp_query->post;,在使用get_the_ID()

然後我們檢查類別,並指定類名的變量$catclass,我們插入了body_class()參數循環獲得ID,請注意使用"代替'

嘗試了這一點:

<?php $loop = new WP_Query(array('post_type' => 'resources', 'posts_per_page' => -1)); ?> 
<?php while ($loop->have_posts()) : $loop->the_post(); ?> 
<?php 
    $catclass = ""; 
    if (in_category('ebook', get_the_ID())) { 
     $catclass="cat1"; 
    } else if(in_category('infographic', get_the_ID())) { 
     $catclass="cat2"; 
    } else if(in_category('casestudy', get_the_ID())){ 
     $catclass="cat3"; 
    } 
?> 

    <div <?php body_class("tile scale-anm {$catclass} all end"); ?>> 
     <section class="small-12 medium-4 large-3 columns end"> 
      <section class="grid"> 
       <figure class="effect-sarah"> 
        <img src="<?php the_field('img'); ?>" alt="img13"/> 
        <figcaption> 
         <h2>Resource</h2> 
         <p class="signika"><?php the_field('desc'); ?></p> 
         <p class="bold"><?php the_field('btnText'); ?></p> 
         <a href="<?php echo esc_url(get_permalink()); ?>" target="_blank"><?php the_field('btnText'); ?></a> 
        </figcaption>   
       </figure> 
      </section> 
     </section> 
    </div> 
<?php 
endwhile; 
wp_reset_postdata(); ?> 

如果你真的感覺活潑,你可以更換if/else陳述與:

switch(true){ 
    case in_category('ebook', get_the_ID()): 
    $catclass = "cat1"; 
    break; 
    case in_category('infographic', get_the_ID()): 
    $catclass = "cat2"; 
    break; 
    case in_category('casestudy', get_the_ID()): 
    $catclass = "cat3"; 
    break; 
    default: 
    $catclass = ""; 
} 
+0

@WheatBreak它太漂亮了....謝謝!那完美的工作!對此,我真的非常感激。還有一件事,我正在使用; data-rel =「cat1」來過濾頁面上的項目,但我不認爲這將工作了。有沒有一種方法來過濾使用這個新系統的頁面上顯示的內容? – Furlong

+0

這是模糊的...我有三個按鈕,每個類別一個。一旦點擊不在所選類別中的帖子類型隱藏。我使用data-rel,當它只是HTML時,它工作得很好。不是我轉到自定義帖子類型。我需要找到一種方法來過濾按鈕點擊中的項目。提前致謝! – Furlong

+0

不知道我是否完全理解,但是如果你使用jQuery,你可以像jQuery(「.cat1」)一樣隱藏它們。hide()' – WheatBeak

相關問題