Excludeți Postarea Curentă din Bucla Postărilor Recente
Care ar fi cea mai bună modalitate de a exclude postarea curentă pe care o vizualizez din această interogare pentru postări recente? Mulțumesc!
<?php
global $post;
if (in_category('top-lists')) {
$myposts2 = get_posts('numberposts=5&offset=0&category=7&post__not_in=array('.get_the_ID().')');
}
else if (in_category('playlists') || in_category('playlistall')) {
$myposts2 = get_posts('numberposts=5&offset=0&category=6,37&post__not_in=array('.get_the_ID().')');
}
else if (in_category('news') || in_category('news')) {
$myposts2 = get_posts('numberposts=5&offset=0&category=95&post__not_in=array('.get_the_ID().')');
}
else {
$myposts2 = get_posts('numberposts=5&offset=0&category=-6,-7,-37,-95,-177&post__not_in=array('.get_the_ID().')');
}
foreach($myposts2 as $post) :
?>
@kaiser și Brian - Mulțumesc pentru răspuns, am introdus codul dar primesc o eroare de 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

În prezent folosesc asta <?php $catquery = new WP_Query( 'cat=1&posts_per_page=5' ); ?>
. Cum implementez varianta ta în a mea? Mulțumesc!

Adaugă acest cod în array-ul tău $args
'post__not_in' => array( get_the_ID() )
În acest fel nu va trebui să te ocupi de obținerea ID-ului postului curent și vei evita potențialele erori legate de obținerea ID-ului. Funcția get_the_ID() obține automat ID-ul pentru tine, așa că nu trebuie să te mai ocupi tu de acest lucru.

Adaugă codul de mai jos în fișierul functions.php al temei active
function be_exclude_current_post( $args ) {
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' );
