最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to show only specific tag in wordpress loop

programmeradmin2浏览0评论

I am trying to get the loop to show only two tag that have the ID 53 and 52. the code without the "if is_tag( array( 53, 52 ) ){" works. I tried different thing but I can't get it to work.

Thank you in advance your your help.

<?php   
if is_tag( array( 53, 52 ) ){
   while ( $projects->have_posts() ) { $projects->the_post(); ?>
         <div <?php post_class(); ?>>
              <a href="<?php the_permalink(); ?>" class="thumb">
                 <?php the_post_thumbnail( $image_size ); ?>
                 <div class="portfolio-hover">
                     <div class="portfolio-description">
                         <h4><?php the_title(); ?></h4>
                         <div><?php the_excerpt(); ?></div>
                     </div>
                 </div>
              </a>
         </div>
      <?php   
   }
}

I am trying to get the loop to show only two tag that have the ID 53 and 52. the code without the "if is_tag( array( 53, 52 ) ){" works. I tried different thing but I can't get it to work.

Thank you in advance your your help.

<?php   
if is_tag( array( 53, 52 ) ){
   while ( $projects->have_posts() ) { $projects->the_post(); ?>
         <div <?php post_class(); ?>>
              <a href="<?php the_permalink(); ?>" class="thumb">
                 <?php the_post_thumbnail( $image_size ); ?>
                 <div class="portfolio-hover">
                     <div class="portfolio-description">
                         <h4><?php the_title(); ?></h4>
                         <div><?php the_excerpt(); ?></div>
                     </div>
                 </div>
              </a>
         </div>
      <?php   
   }
}
Share Improve this question asked Oct 7, 2019 at 20:51 15eme Doctor15eme Doctor 1232 silver badges7 bronze badges 1
  • What's the context here? Is this loop the main loop for a post type archive or similar, or a separate secondary loop? – Jacob Peattie Commented Oct 7, 2019 at 23:43
Add a comment  | 

2 Answers 2

Reset to default 0

Here are two alternative for you based on your actual query. If you're querying $projects->have_posts() on WP default blog (Post) you can use query like

$projects = new WP_Query( array( 'tag__in' => array( 52, 53 ) ) ); 

Another is if you're querying on Custom Post Type you can use following query to get posts or your desire tags.

$array = array (
'post_type' => 'your custom post type',
'posts_per_page' => 10,
// Your other criteria of the query
'tax_query' => array(
    array(
        'taxonomy' => 'name of your tag taxonomy',
        'field' => 'id',
        'terms' => array(52,53)
    )
);

$projects = new WP_Query( $array );

if ( $projects->have_posts() ){
   while ( $projects->have_posts() ) { $projects->the_post(); ?>
         <div <?php post_class(); ?>>
              <a href="<?php the_permalink(); ?>" class="thumb">
                 <?php the_post_thumbnail( $image_size ); ?>
                 <div class="portfolio-hover">
                     <div class="portfolio-description">
                         <h4><?php the_title(); ?></h4>
                         <div><?php the_excerpt(); ?></div>
                     </div>
                 </div>
              </a>
         </div>
      <?php   
   }
}

You can set a query only for specified tags:

$query = new WP_Query( 'tag_id=52,53' );

// Loop
while ( $the_query->have_posts() ) :
    $the_query->the_post();
    echo '<h1>' . get_the_title() . '</h1>';
    echo '<p>' . the_excerpt() . '</p>';
endwhile;

// Reset query
wp_reset_query();
wp_reset_postdata();
发布评论

评论列表(0)

  1. 暂无评论