wordpress get_terms和WP_Query无法按预期工作

时间:2017-09-11 12:14:04

标签: php wordpress footer

我为我的网站创建了一个版权页,其中我发布了我拥有的不同赞助商的所有徽标。我通过自定义帖子类型添加所有赞助商。我还添加了一个特定的自定义分类法来区分不同类型的赞助。

我在footer.php中使用此代码来显示它们:

<?php $terms = get_terms('sponsor_tipology'); 
$count = count( $terms );
if ( $count > 0 ) {
   foreach ( $terms as $term ) { ?>
      <div class="col-xs-12 <?php echo $term->slug ;?>">      
         <h3><?php  echo $term->name;?></h3> 
         <?php $arg = array (
               'post_type' => 'colophone', 
               'post_per_page' => -1,
               'sponsor_edition' => 'current',
               'sponsor_tipology' => $term->slug,
               );

         $pesca_post = new WP_Query ($arg);
         $quanti_post = $pesca_post->post_count;

         if(have_posts()){ 
            while ($pesca_post->have_posts()) : $pesca_post->the_post();
               $featured = get_the_post_thumbnail_url(get_the_ID(),'large');

               if ($quanti_post == 5){
                  $classe_bootstrap = 15;
               }elseif ($quanti_post > 5){
                  $classe_bootstrap = "2 text-center";
               }elseif($quanti_post < 5){
                  $classe_bootstrap = 12/$quanti_post;
               }

              echo '<div class="col-md-' . $classe_bootstrap . '">'; 
                 if (isset($featured)){
                    $img = $featured;
                 }else{
                    $img = get_template_directory_uri() . '/img/placeholder.png';
                 } ?>
               <a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($post->ID);?>" >
                  <div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
               </a>
              <?php   echo '</div>';
      endwhile;
     }?>
    </div>
<?php }
}?>

我的问题是这段代码完全在某些页面上工作,另一方面它显示内容,避免属于第一个词的内容,无论它是什么。

我注意到它适用于我使用其他查询的pagaes。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我以这种方式改变它,现在它正在工作!

$terms = get_terms('sponsor_tipology');
    $count = count( $terms );  
       if ( $count > 0 ) { 
          foreach ( $terms as $term ) {   //per ogni termine presente 
             $nome = $term->slug;?>
             <div class="col-xs-12 <?php echo $term->slug ;?>">      
                <h3><?php  echo $term->name;?></h3> 
                <?php $arg = array (
                   'post_type' => 'colophone', 
                   'post_per_page' => -1,
                   'sponsor_edition' => 'current',
                   'sponsor_tipology' => $nome,
                   );

                   $elementi = get_posts($arg);
                   $quanti_post = count( $elementi );

                   if ($quanti_post == 5){
                       $classe_bootstrap = 15;
                   }
                   elseif ($quanti_post > 5){
                       $classe_bootstrap = "2 text-center";
                   }
                   elseif($quanti_post < 5){
                       $classe_bootstrap = 12/$quanti_post;
                   }

                   foreach($elementi as $elemento){
                       $featured = get_the_post_thumbnail_url($elemento->ID,'large'); 
                       if (isset($featured)){
                          $img = $featured;
                       }
                       else{
                          $img = get_template_directory_uri() . '/img/placeholder.png';
                       } ?>
                       <div class="col-md-<?php echo $classe_bootstrap; ?>"> 
                          <a href="<?php echo esc_attr(get_permalink($msd_settings['partner_page'])); ?>" title="<?php echo get_the_title($elemento->ID);?>" >
                             <div class="col-xs-12" style="background-image:url(<?php echo esc_url($img); ?>); height:100px;background-size:contain;background-repeat:no-repeat;background-position:center center;"></div>
                          </a>
                       </div>
          <?php  }?>
         </div>
   <?php    }
}?>