簡體   English   中英

wordpress get_terms和WP_Query無法按預期工作

[英]wordpress get_terms and WP_Query not working as expected

我為我的網站創建了一個Colophon,在其中張貼了我擁有的不同贊助商的所有徽標。 我通過自定義帖子類型添加所有贊助者。 我還添加了特定的自定義分類法,以區分贊助的不同類型。

我在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 }
}?>

我的問題是,此代碼僅在某些頁面上可以完全使用,而在其他頁面上則可以顯示其內容,而不必考慮屬於第一個術語的內容。

我注意到它可以在使用其他查詢的地方使用。

我究竟做錯了什么?

我以這種方式進行了更改,現在可以正常使用了!

$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    }
}?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM