Now on the front page, in the part with post excerptions I have such a code:
<div class="wiadPodtyt">
<ul>
<?php
$piwa = get_posts(
array(
'posts_per_page' => 2,
'cat' => 81,
'post__not_in' => $news_ids,
'offset' => 1
)
);
foreach ( $piwa as $post ) {
setup_postdata( $post );
?>
<li><a href='<?php the_permalink(); ?>' class="subtytulyWiad"><?php the_title(); ?></a> </li>
It's used to avoid double posts, because in other place i show the newest posts from mostly all categories. I'd like to shorten this code, because i repeat it some times (in div class wiadPodtyt). The problem is this function is changed with such variables like number of posts (posts_per_page) and category id (cat). How to write it corectly?
Update
In response to @totels, I tried this:
function print_alko($allpost=8, $postpage=1, $postcat=81)
{
$news = get_posts(
array(
'posts_per_page' => $allpost,
'cat' => -88
)
);
// Get an array with just the IDs of the posts in the $fruit array
$news_ids = wp_list_pluck( $news, 'ID' );
//'post__not_in' => $news_ids powinno by? po cat=>...
$piwo = get_posts(
array(
'posts_per_page' => $postpage,
'cat' => $postcat,
'post__not_in' => $news_ids
)
);
foreach ( $piwo as $post ) {
setup_postdata( $post );
echo '<div class="alpha wiadImg_a">';
the_post_thumbnail();
echo '<!-- koniec wiadImg_a --></div>';
echo '<div class="alko"><div class="wiadTyt_a">';
echo '<h1 class="wiadTyt">';
echo "<a href="."/'"."the_permalink()"."/'";
echo 'class=/"wiadTyt_a/">'.the_title().'</a></h1><!-- koniec wiadTyt_a --></div>';
}
echo '<div class="wiadPodtyt"><ul>';
$piwa = get_posts(
array(
'posts_per_page' => 2,
'cat' => $postcat,
'post__not_in' => $news_ids,
'offset' => 1
)
);
foreach ( $piwa as $post ) {
setup_postdata( $post );
echo "<li><a href=/'/";
the_permalink();
echo "/'";
echo 'class=/"subtytulyWiad/">';
the_title();
echo '</a></li>';
}
wp_reset_query();
echo '</ul><!-- koniec wiadPodtyt --></div><!-- koniec alko #prawe pude?ko --></div>';
//koniec fcji print_alko
}
and the result is only 1 last news, graphically corect, but wrong content. Something is wrong with the wp_query - it makes wrong query.