1

I want the one most view post, with custom field value, in 4 categories (foreach). It works. But as I use my code for a slider, I want to echo and active css class for the first one post of my loop.

I use a php counter, but it doesn't work. What I doing wrong. Here is my code

$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 Now the right code. Thanks a lot bynicolas

$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; ?> 
  • If all is working good, just mark this question as solve by using the check mark beside my answer – bynicolas Aug 31 '16 at 17:48
  • 1
    Done bynicolas ;) – Benoît Aug 31 '16 at 19:05
  • I added some info to the chat – bynicolas Sep 1 '16 at 5:43
0

EDIT

Ok so after better understanding your issue, I would not use a counter but a true/false flag that we switch when it's been used. Try this code.

$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

You don't need a counter to do the job, you can use the current_post property of the current object which returns the index of the current object

so instead of using

if ($i == 0)

try

if( 0 == $query->current_post )

and get rid of your counters

  • Comments are not for extended discussion; this conversation has been moved to chat. – fuxia Aug 31 '16 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.