Escludere il Post Corrente dal Loop dei Post Recenti
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) :
?>
@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

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

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.

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' );
