G
G
godsplane2020-04-29 12:25:12
WordPress
godsplane, 2020-04-29 12:25:12

How to get rid of an empty loop without which nothing works?

I'm tagging posts.

<div class="portfolio-wrapper row j-center container">
  <?php 
$tags = get_the_tags();
foreach ( $tags as $tag ){
  
}
   $lastposts = get_posts( array(
  'numberposts' => -1,
  'tag' => $tag->slug,
  'category'    => 0,
  'orderby'     => 'menu_order',
  'order'       => 'DESC',
  'include'     => array(),
  'exclude'     => array(),
  'meta_key'    => '',
  'meta_value'  =>'',
  'post_type'   => 'portfolio',
  'suppress_filters' => true, // подавление работы фильтров изменения SQL запроса
) );
   foreach( $lastposts as $post ){ setup_postdata($post);
     ?>

  <div class="portfolio-item">
    <h2>
      <? the_title() ?>
    </h2>
    <a class="fancybox" rel="group" href="<?php the_post_thumbnail_url('large')?>">
      <img src="<?php the_post_thumbnail_url('medium')?>" alt="Фотография">
    </a>


  </div>

  <?php 
     
   }
   wp_reset_postdata()
   ?>
</div>
</div>


Nothing works without it
$tags = get_the_tags();
foreach ( $tags as $tag ){
  
}

But this is an empty loop, how can you get rid of this construction and why does it work like that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rickkk, 2020-04-29
@godsplane

The cycle matters because fills the $tag variable with the last element in the $tags array. And then in the following code, this variable is used like this - 'tag' => $tag->slug,
Try replacing the loop with an expression
$tag = array_pop($tags);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question