Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
You have a 0% accept rate, you should fix this to encourage people to post more answers –  Tom J Nowell Nov 20 '12 at 10:00
    
btw what does it mean 'accept rate'? for me it doesn't matter. –  Marcin Nov 26 '12 at 21:31
    
The accept rate tells people how many of your questions you accepted an answer for. It's not intended for you, rather its for the people answering your questions. A rate of 0% means you have accepted no answers, and that answering your questions is not advised as you won't mark them as accepted, and no reputation will be awarded –  Tom J Nowell Nov 26 '12 at 22:06
    
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 Dec 2 '12 at 11:37
add comment

1 Answer

up vote 0 down vote accepted

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); ?>
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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