Aller au contenu

[Wordpress] éviter l'affichage des doublons


thick

Sujets conseillés

Bonjour à tous,

Je relance sur le Hub un thread que j'avais lancé sur le support WP Francophone car je n'ai pas résolu mon problème :(

Sur l'accueil de Adicie.com , j'affiche les derniers billets parus, ainsi que chaque dernier billet par catégorie.

Du coup, je me retrouve avec des doublons que je voudrais éradiquer.

Le but serait de fabriquer une boucle qui indiquerait que si un billet est paru dans "derniers articles parus", il ne doit pas s'afficher ailleurs.

Au départ, mon code est :

- Pour les derniers articles parus :

   1.
<?php _e('Derniers articles parus'); ?></h3>
2.
<?php query_posts('showposts=3'); ?>
3.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
4.
<?php the_date('j F Y') ?> par <?php the_author() ?> <br />
5.
<a href="<?php the_permalink(); ?>" title="<?php the_title() ?>" class="title"><?php the_title() ?></a>
6.
<?php the_excerpt(); ?>
7.
[ <a href="<?php the_permalink() ?>" rel="bookmark" title="Lien permanent vers <?php the_title(); ?>">Lire la suite</a> ]<br /><br />
8.
<?php endwhile; endif; ?>

- Pour les derniers billets dans leur catégorie :

   1.
<?php
2.
// this is where the Politique module begins
3.
query_posts('showposts=2&cat=10'); ?>
4.
<h3><?php
5.
// this is where the name of the Politique category gets printed
6.
wp_list_categories('include=10&title_li=&style=none'); ?></h3>
7.
<?php while (have_posts()) : the_post(); ?>
8.
<div class="feature"><a href="<?php the_permalink() ?>" rel="bookmark" title="Lien permanent vers <?php the_title(); ?>"></a><a href="<?php the_permalink() ?>" rel="bookmark" class="title">
9.
<?php
10.
// this is where title of the Politique gets printed
11.
the_title(); ?></a>
12.
<?php
13.
// this is where the excerpt of the Politique gets printed
14.
the_excerpt(); ?>
15.
[ <a href="<?php the_permalink() ?>" rel="bookmark" title="Lien permanent vers <?php the_title(); ?>">Lire la suite</a> ]
16.
</div>
17.
<?php endwhile; ?>

En m'aidant de http://codex.wordpress.org/The_Loop et du code suivant, j'ai tenté de modifier pour donner ce qui suit plus loin :

   1.
<?php _e('Derniers articles parus'); ?></h3>
2.
<?php query_posts('showposts=3'); ?>
3.
<?php if (have_posts()) : while (have_posts()) : the_post();
4.
$local_ids[] = $post->ID;
5.
...

   1.
<?php
2.
// this is where the Politique module begins
3.
// passer le nombre de posts demandés de 2 à 5 pour s'assurer d'avoir un nombre suffisant de posts
4.
query_posts('showposts=5&cat=10'); ?>
5.
<h3><?php
6.
// this is where the name of the Politique category gets printed
7.
wp_list_categories('include=10&title_li=&style=none'); ?></h3>
8.
<?php while (have_posts()) : the_post();
9.
if(in_array($local_ids,$post->ID)) continue;
10.
else $post_counter++;
11.
...l'affichage du billet...
12.
if($post_counter > 2) break;
13.
<?php endwhile; ?>

Et donc je fais ça :

#
#
<div class="feature clearfloat" id="lead">
#
<h3><?php _e('Derniers articles parus'); ?></h3>
#
<?php if (have_posts()) : while (have_posts()) : the_post();
#
if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
#
<?php the_date('j F Y') ?> par <?php the_author() ?> <br />
#
<a href="<?php the_permalink(); ?>" title="<?php the_title() ?>" class="title"><?php the_title() ?></a>
#
<?php the_excerpt(); ?>
#
[ <a href="<?php the_permalink() ?>" rel="bookmark" title="Lien permanent vers <?php the_title(); ?>">Lire la suite</a> ]<br /><br />
#
<?php endwhile; endif; ?>
#
</div><!--END FEATURE-->

#
<div id="leftcol">
#

#
<h3><?php
#
// this is where the name of the France category gets printed
#
wp_list_categories('include=5&title_li=&style=none'); ?></h3>
#
<?php $my_query = new WP_Query('category_name=france&showposts=1');
#
while ($my_query->have_posts()) : $my_query->the_post();
#
$do_not_duplicate = $post->ID; ?>
#
<div class="feature"><a href="<?php the_permalink() ?>" rel="bookmark" title="Lien permanent vers <?php the_title(); ?>"></a><a href="<?php the_permalink() ?>" rel="bookmark" class="title">
#
<?php
#
// this is where the title of the France gets printed
#
the_title(); ?></a>
#
<?php
#
// this is where the excerpt of the France gets printed
#
the_excerpt(); ?>
#
[ <a href="<?php the_permalink() ?>" rel="bookmark" title="Lien permanent vers <?php the_title(); ?>">Lire la suite</a> ]
#
</div>
#
<?php endwhile; ?>

Mais niet, les doublons demeurent au garde-à-vous !

Dan m'a dit que nous avions de grands spécialistes WP sur le Hub, donc j'espère qu'un ou deux passeront par ici pour me sortir de cette impasse.

Merci d'avance

Lien vers le commentaire
Partager sur d’autres sites

Pas bête, mais je peine encore à voir comment coder ça vu que mes compétences sont minimalistes.

Tu ne voudrais pas me montrer un petit bout du chemin ? Ca serait sympa :)

Lien vers le commentaire
Partager sur d’autres sites

Salut,

T'as rajouté ca ? :

$do_not_duplicate = $post->ID;

A priori, comme ca, tu récupères le dernier $post->ID.

Or il ne te faut pas le dernier, mais il te les faudrait tous.

Pour cela, l'idéal serait de les stocker dans un tableau.

Ensuite, au lieu de vérifier si c'est égal :

if( $post->ID == $do_not_duplicate ) continue;

Tu vérifies si c'est 'dans le tableau'.

D'ailleurs, tu peux même vérifier si ' ca n'est pas dans le tableau' > Si ca ne l'est pas, alors tu l'affiches (sinon, sous entendu, tu zappes. ;) )

Ca donnerait quelque chose comme ca :

  $do_not_duplicate[] = $post->ID;

puis :

  if( in_array($do_not_duplicate , $post->ID)){
// Là, tu mets tout ce qu'il y a dans la boucle while..
};

Voilà.

Dsl pour le temps de réponse, mais à par au cyber, sinon j'ai pas d'adsl.. :D

( une p'tite semaine encore.. )

Lien vers le commentaire
Partager sur d’autres sites

  • 9 years later...

Hello je me retrouves exactement avec le même probléme!

Mais posts sont en doublon , chaque post appartient à plusieurs catégories.
J'ai teste ce que hubmaster a suggérer plus haut , même avant à l'aide du codex , je dois certainement mal m'y prendre. Du coup j'aurais aimée connaitre la suite de votre dusussion ^^

 

Voila mon code:

<?php
/**
 * The template for displaying a grid item.
 *
 * @package WordPress
 * @subpackage Edition
 * @since Edition 1.0
 */

    $query = new WP_Query( array( 'category__not_in' => array( 32 ) ) );
?>
    

    <?php $do_not_duplicate[] = $post->ID; ?>
    
    <?php if( in_array($do_not_duplicate , $post->ID)) : ?>
    <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();?>



<?php

global
$tw_section, // section object
$grid_size, // grid size
$post;       // section object

$section_id    = get_the_ID($post);  // section id
$tw_categories = get_categories();   // get all post categories
$post_type     = get_post_type();


$thumbnail_size = 'half';
$thumbnail_size = ($grid_size == 'fullgrid') ? 'big' : $thumbnail_size;
if ($grid_size == 'related') {
    $thumbnail_size = 'related';
} ?>

<div class="<?php if ( isset($tw_categories[0]->term_id) ) { echo 'highlight_category_' . $tw_categories[0]->term_id; } ?> <?php echo (has_post_thumbnail()) ? 'has-thumbnail' : 'no-thumbnail'; ?> <?php echo $grid_size;?> grid-item">
    <div class="isobrick-inner">
        <?php
            the_post_thumbnail($thumbnail_size, array(
                'onload' => 'jQuery(this).animate({ "opacity" : 0.75 }, 500);'
            ));
        ?>

        <?php echo themewich_review_circle('none', false); ?>

        <?php if ( 'post' == $post_type ) : ?>
            <span class="thumboverdate">
                <i class="fa fa-bookmark"></i>
                <?php themewich_date('human'); ?>
            </span>
        <?php else: ?>
            <?php if ( function_exists( 'wc_get_template' ) ) : ?>
                <span class="thumboverdate">
                    <?php wc_get_template( 'loop/price.php' ); ?>
                </span>
            <?php endif; ?>
        <?php endif; ?>

        <div class="thumbovertext">
            <div class="badge">
                <?php echo ag_get_cats(3); ?>
            </div>
            <h2 class="title">
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                    <?php
                        $title = get_the_title();
                        if (strlen($title) >= 75) {
                             $title = mb_substr($title, 0, 75) . "... ";
                        }
                        echo $title;
                    ?>
                </a>
            </h2>

            <?php if ( 'post' == $post_type ) : ?>
                <?php preg_match('/<!--more(.*?)?-->/', $post->post_content, $matches); ?>
                <a class="button outline" href="<?php the_permalink(); ?>"  title="<?php the_title_attribute(); ?>">
                    <?php
                        if ( isset($matches[1]) && $matches[1] ) {
                            echo $matches[1];
                        } else {
                            _e('Read More', 'themewich');
                        }
                    ?>
                </a>
            <?php endif; ?>

        </div>

        <a class="brick-thumb-link" href="<?php the_permalink(); ?>"  title="<?php the_title_attribute(); ?>">
            <?php the_title(); ?>
        </a>
    </div>
</div>



<?php endwhile; ?>
<?php endif; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>

 

Lien vers le commentaire
Partager sur d’autres sites

Veuillez vous connecter pour commenter

Vous pourrez laisser un commentaire après vous êtes connecté.



Connectez-vous maintenant
×
×
  • Créer...