2017-07-26 41 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

相关问题