Counter in loop in foreach

时间:2016-08-31 作者:Benoît

我想要那个most view post, 具有自定义字段值,分为4类(foreach).它起作用了。但当我使用我的代码作为滑块时,我想echo 以及我循环的第一篇帖子的活动css类。

我使用php计数器,但它不工作。我做错了什么。这是我的密码

$cats = array(\'batiments-delevage\', \'logements-bovin\', \'stockages-deffluents\', \'soins-et-hygiene\');
$exclude_posts = array();
foreach( $cats as $cat ) :
  // build query argument
  $query_args = array(
    \'category_name\' => $cat,
    \'showposts\'     => 1,
    \'post_type\'     => \'post\',
    \'post_status\'   => \'publish\',
    \'meta_key\'      => \'wpb_post_views_count\', //custom field for view count
    \'orderby\'       => \'meta_value_num\',
    \'order\'         => \'DESC\'
  );

  // exclude post that already have been fetched
  // this would be useful if multiple category is assigned for same post
  if( !empty($exclude_posts) )
    $query_args[\'post__not_in\'] = $exclude_posts;

  // do query
  $query = new WP_Query( $query_args );

  if ($query->have_posts()) : // check if query have any post

    $i = 0; // start a counter for the active indicator

    while ($query->have_posts()) : $query->the_post(); // start loop & set post global 
      // Get category slug parent info
      $category = get_the_category();
      $cat_slug = $category[0]->slug;
      $cat_name = $category[0]->name;
      $cat_url =  get_category_link( $category[0]->cat_ID );
      $post_date_month = get_the_date(\'M\');
      $post_date_year = get_the_date(\'Y\');

      $exclude_posts[] = get_the_ID();

?><div class="item <?php if ($i == 0) echo \'active\'; ?>">
  <header class="header-item">
    <a class="link-img" href="<?php the_permalink(); ?>">
      <?php the_post_thumbnail(\'tab-slider\', array( \'class\' => \'img-responsive\') ); ?>
    </a>
    <div class="date-box">
      <?php
      echo $post_date_month.\'<br/>\';
      echo \'<small>\'.$post_date_year.\'</small>\';
    ?></div>

    </header><!-- /header -->
    <div class="carousel-caption">
      <h4><?php if ($i == 0) echo \'active\'; ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
      <p class="meta-cat"><small><span class="inline-categ-icon visible-xs-block visible-sm-block icon-<?php echo $cat_slug;?>"></span>  Dans <a href="<?php echo $cat_url;?>" title="Voir les articles dans la rubrique « <?php echo $cat_name;?> » "><?php echo $cat_name;?></a></small></p>
      <p class="most-view-excerpt"><?php echo excerpt(27); ?></p>
    </div>
    <p><a class="read-more" href="<?php the_permalink(); ?>" title="Lire notre article <?php the_title(); ?>">Lire le reportage</a></p>
  </div>
  <?php $i++; endwhile; endif; ?>

<?php endforeach; ?> 
</div> 
Updated现在是正确的代码。非常感谢Nicolas

$cats = array(\'batiments-delevage\', \'logements-bovin\', \'stockages-deffluents\', \'soins-et-hygiene\');
$exclude_posts = array();
$active = true;
foreach( $cats as $cat ) :
  // build query argument
  $query_args = array(
  \'category_name\'       => $cat,
  \'posts_per_page\'      => 1,
  \'post_type\'           => \'post\',
  \'post_status\'         => \'publish\',
  \'meta_key\'            => \'wpb_post_views_count\', //custom field for view count
  \'orderby\'             => \'meta_value_num\',
  \'order\'               => \'DESC\'
);

// exclude post that already have been fetched
// this would be useful if multiple category is assigned for same post
if( !empty($exclude_posts) )
  $query_args[\'post__not_in\'] = $exclude_posts;

  // do query
  $query = new WP_Query( $query_args );

  if ($query->have_posts()) : // check if query have any post

    while ($query->have_posts()) : $query->the_post(); // start loop & set post global 
      // Get category slug parent info
      $category = get_the_category();
      $cat_slug = $category[0]->slug;
      $cat_name = $category[0]->name;
      $cat_url =  get_category_link( $category[0]->cat_ID );
      $post_date_month = get_the_date(\'M\');
      $post_date_year = get_the_date(\'Y\');


      $exclude_posts[] = get_the_ID();

?><div itemid="<?php the_permalink(); ?>" itemscope class="<?php if( $active ){ echo \'active\'; $active = false; } ?>" style="position :relative;" itemtype="http://schema.org/Article">
<header class="header-item">
  <a class="link-img" href="<?php the_permalink(); ?>">
      <?php the_post_thumbnail(\'tab-slider\', array( \'class\' => \'img-responsive\') ); ?>
  </a>
  <div class="date-box"><?php
      echo $post_date_month.\'<br/>\';
      echo \'<small>\'.$post_date_year.\'</small>\';
  ?></div>
  </header><!-- /header -->
  <div class="carousel-caption"> 
    <h4  itemprop="name"><?php if( $active ){ echo \'active\'; $active = false; } ?><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
    <p class="meta-cat"><small><span class="inline-categ-icon visible-xs-block visible-sm-block icon-<?php echo $cat_slug;?>"></span>  Dans <a href="<?php echo $cat_url;?>" title="Voir les articles dans la rubrique « <?php echo $cat_name;?> » "><?php echo $cat_name;?></a></small></p>
    <p class="most-view-excerpt"><?php echo excerpt(27); ?></p>
    <?php $meta = get_post_meta($post->ID, \'wpb_post_views_count\', true);?>
    <div itemprop="interactionStatistic" itemscope itemtype="http://schema.org/InteractionCounter">
      <link itemprop="interactionType" href="http://schema.org/WatchAction"/>
      <meta itemprop="userInteractionCount" content="<?php echo $meta; ?>" />
    </div>
  </div>
  <p><a class="read-more" href="<?php the_permalink(); ?>" title="Lire notre article <?php the_title(); ?>">Lire le reportage</a></p>
</div>
<?php endwhile; endif; ?>
<?php endforeach; ?> 

1 个回复
最合适的回答,由SO网友:bynicolas 整理而成

EDIT

好的,在更好地理解您的问题之后,我不会使用计数器,而是使用真/假标志,在使用时切换。请尝试此代码。

$cats = array(\'batiments-delevage\', \'logements-bovin\', \'stockages-deffluents\', \'soins-et-hygiene\');
$exclude_posts = array();
$active = true;
foreach( $cats as $cat ) :
  // build query argument
  $query_args = array(
  \'category_name\' => $cat,
  \'posts_per_page\'=> 1,
  \'post_type\'     => \'post\',
  \'post_status\'   => \'publish\',
  \'meta_key\'      => \'wpb_post_views_count\', //custom field for view count
  \'orderby\'       => \'meta_value_num\',
  \'order\'         => \'DESC\'
);

// exclude post that already have been fetched
// this would be useful if multiple category is assigned for same post
if( !empty($exclude_posts) )
  $query_args[\'post__not_in\'] = $exclude_posts;

// do query
$query = new WP_Query( $query_args );

if ($query->have_posts()) : // check if query have any post

  while ($query->have_posts()) : $query->the_post(); // start loop & set post global 
    // Get category slug parent info
    $category = get_the_category();
    $cat_slug = $category[0]->slug;
    $cat_name = $category[0]->name;
    $cat_url =  get_category_link( $category[0]->cat_ID );
    $post_date_month = get_the_date(\'M\');
    $post_date_year = get_the_date(\'Y\');


    $exclude_posts[] = get_the_ID();

?><div class="item <?php if( $active ){ echo \'active\'; $active = false; } ?>">
  <header class="header-item">
    <a class="link-img" href="<?php the_permalink(); ?>">
      <?php the_post_thumbnail(\'tab-slider\', array( \'class\' => \'img-responsive\') ); ?>
    </a>
    <div class="date-box"><?php
      echo $post_date_month.\'<br/>\';
      echo \'<small>\'.$post_date_year.\'</small>\';
  ?></div>
  </header><!-- /header -->
    <div class="carousel-caption"> 
      <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
      <p class="meta-cat"><small><span class="inline-categ-icon visible-xs-block visible-sm-block icon-<?php echo $cat_slug;?>"></span>  Dans <a href="<?php echo $cat_url;?>" title="Voir les articles dans la rubrique « <?php echo $cat_name;?> » "><?php echo $cat_name;?></a></small></p>
      <p class="most-view-excerpt"><?php echo excerpt(27); ?></p>
    </div>
    <p><a class="read-more" href="<?php the_permalink(); ?>" title="Lire notre article <?php the_title(); ?>">Lire le reportage</a></p>
  </div>
<?php endwhile; endif; ?>

<?php endforeach; ?>

ORIGINAL

你不需要计数器来完成这项工作,你可以使用current_post 返回当前对象索引的当前对象的属性

所以不用

if ($i == 0)

尝试

if( 0 == $query->current_post )

然后把你的计数器处理掉