简体   繁体   English

wordpress get_terms和WP_Query无法按预期工作

[英]wordpress get_terms and WP_Query not working as expected

i've created a colophon for my website in which i post all the logos of the different sponsors i have. 我为我的网站创建了一个Colophon,在其中张贴了我拥有的不同赞助商的所有徽标。 I add all the sponsors via custom post type. 我通过自定义帖子类型添加所有赞助者。 i also added a specific custom taxonomy to distinguish between the different typologies of sponsorships. 我还添加了特定的自定义分类法,以区分赞助的不同类型。

I use this code in the footer.php to display them: 我在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 }
}?>

my problem is that this code is completely working just on some pages, on other it shows the contents avoiding the ones belonging to the first term, no matter which it will be. 我的问题是,此代码仅在某些页面上可以完全使用,而在其他页面上则可以显示其内容,而不必考虑属于第一个术语的内容。

I have noticed that it works in pagaes where i use other queries. 我注意到它可以在使用其他查询的地方使用。

What am i doing wrong? 我究竟做错了什么?

i changed it in this way and now it's working! 我以这种方式进行了更改,现在可以正常使用了!

$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