Escludere il Post Corrente dal Loop dei Post Recenti

18 set 2011, 00:13:16
Visualizzazioni: 51.9K
Voti: 12

Qual è il modo migliore per escludere il post che sto attualmente visualizzando da questa query dei post recenti?

<?php
            global $post;
            // Ottieni l'ID del post corrente
            $current_post_id = $post->ID;
            
            if (in_category('top-lists')) {
                $myposts2 = get_posts('numberposts=5&offset=0&category=7&post__not_in=' . array($current_post_id));
            }
            else if (in_category('playlists') || in_category('playlistall')) {
                $myposts2 = get_posts('numberposts=5&offset=0&category=6,37&post__not_in=' . array($current_post_id));
            }
            else if (in_category('news') || in_category('news')) {
                    $myposts2 = get_posts('numberposts=5&offset=0&category=95&post__not_in=' . array($current_post_id));
            }
            else {
                $myposts2 = get_posts('numberposts=5&offset=0&category=-6,-7,-37,-95,-177&post__not_in=' . array($current_post_id));
            }

            foreach($myposts2 as $post) :
            ?>
0
Tutte le risposte alla domanda 3
7
38

L'argomento post__not_in dovrebbe funzionare perfettamente per te:

$args = array(
    'numberposts' => 5,
    'offset' => 0,
    'category' => 7,
    'post__not_in' => array( $post->ID )
);
$myposts2 = get_posts($args);
18 set 2011 04:08:25
Commenti

@kaiser e Brian - Grazie per avermi risposto, ho inserito il codice ma sto ricevendo un errore di map_array ----

Warning: array_map() [function.array-map]: Argument #2 should be an array in /home/sitemain/public_html/wp-includes/query.php on line 1709

Warning: implode() [function.implode]: Invalid arguments passed in /home/sitemain/public_html/wp-includes/query.php on line 1709

Chad Chad
19 set 2011 20:38:33

Prova l'ultima modifica. Inserisci i tuoi argomenti in un array.

Brian Fegter Brian Fegter
19 set 2011 21:01:03

Hm? Perché io? @Brian Fegter ha già risposto. :)

kaiser kaiser
19 set 2011 21:08:44

ha :) Sto bene. Sono sempre felice di aiutare.

Brian Fegter Brian Fegter
19 set 2011 21:22:43

Perfetto, grazie a tutti soprattutto a @BrianFegter!

Chad Chad
21 set 2011 06:53:55

Attualmente sto usando questo <?php $catquery = new WP_Query( 'cat=1&posts_per_page=5' ); ?>. Come posso integrare il tuo nel mio? Grazie!

Jornes Jornes
10 apr 2020 19:19:35

@Jornes Puoi semplicemente usare 'cat' => 1, 'posts_per_page' => 5 nell'esempio sopra.

Brian Fegter Brian Fegter
14 apr 2020 00:05:28
Mostra i restanti 2 commenti
0

Aggiungi questo ai tuoi $args

'post__not_in' => array( get_the_ID() )

In questo modo non dovrai preoccuparti di ottenere l'ID del post corrente e potenzialmente eviterai errori nel recupero del tuo ID. La funzione get_the_ID() ottiene semplicemente l'ID per te, così non dovrai gestirlo o fare nulla.

3 dic 2018 06:17:08
0

Aggiungi il codice seguente nel file functions.php del tema attivo

    function be_exclude_current_post( $args ) {
        // Esclude il post corrente dai widget degli articoli
        if( is_singular() && !isset( $args['post__in'] ) )
            $args['post__not_in'] = array( get_the_ID() );
        return $args;
    }
    add_filter( 'widget_posts_args', 'be_exclude_current_post' );
28 mar 2017 15:12:38