0

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.

1
  • OK, finally I understand how to accept answers, for now I'was sured that click on "Was this post useful to you? Yes" is enough. Is that ok?
    – Marcin
    Commented Dec 2, 2012 at 11:37

1 Answer 1

0

You can make your own function to minimize the characters typed for each time you want to repeat the call to get_posts:

in functions.php (perhaps):

function print_piwa($postcount=2, $cat_id=81) {
    ?>
    <div class="wiadPodtyt">
        <ul>
            <?php 
            $piwa = get_posts(array(
                'posts_per_page' => $postcount,
                'cat' => $cat_id,
                '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>
                <?php
            }
            ?>
        </ul>
    </div>
    <?php
}

You'll obviously need to figure out what variables you want to set and use them accordingly, but there's no reason you can't use get_posts in a function of your own making.

Then you can just call your function in your templates:

<?php print_piwa(5, 81); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.