Je veux celui qui affiche le plus de vues , avec une valeur de champ personnalisée, dans 4 catégories (foreach
). Ça marche. Mais comme j'utilise mon code pour un curseur, je veux echo
et la classe css active pour le premier post de ma boucle.
J'utilise un compteur php, mais ça ne marche pas. Ce que je fais mal. Voici mon 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>
Mis à jour Maintenant le bon code. Merci beaucoup 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; ?>
MODIFIER
Ok, donc, après avoir mieux compris votre problème, je n’utiliserais pas un compteur mais un indicateur vrai/faux que nous changerons lorsqu’il a été utilisé. Essayez ce 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
Vous n'avez pas besoin d'un compteur pour faire le travail, vous pouvez utiliser la propriété current_post
de l'objet actuel qui renvoie l'index de l'objet actuel
donc au lieu d'utiliser
if ($i == 0)
essayer
if( 0 == $query->current_post )
et se débarrasser de vos compteurs