0

How can I make 10 posts of one category. What query should I use.

Here in following query I need its limit of post in per page.

<div id="container">
        <div id="content" class="category">
                <?php if ( have_posts() ) : ?>

            <h1 class="page-title">Category Archives: <span><?php echo  single_cat_title(); ?></span></h1>

            <div class="archive-meta"><?php echo category_description(); ?> </div>
  <div class="post">
            <?php while ( have_posts() ) : the_post(); ?>
                <h2 id="post-2451">
                    <a href="<?php the_permalink();?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <?php the_title();?></a></h2>
            <?php endwhile;?>

                <?php endif; ?>

EDIT

I meant to say that I want 10 posts in category.php . Let suppose I am clicking one category in categories so it should show result 10 post of each category.

  • Please clean up the code you posted. Also: Please clarify what your question is. Do you want to retrieve 10 post of a category? What have you tried so far? – kraftner May 5 '14 at 8:37
  • You were active an hour ago and still haven't updated your question as @kraftner asked. How do you expect anyone to answer your question. Do you need 10 posts on a category.php type template, or 10 posts from a specific category on a page.php type template, or just 10 posts generally on a category page – Pieter Goosen May 5 '14 at 11:52
  • @PieterGoosen Please be patient. Not updated in an hour... not really a reason to push :) – kaiser May 5 '14 at 12:13
  • 1
    LOL, ok @kaiser will use a little more patience :) – Pieter Goosen May 5 '14 at 12:18
  • I meant to say that I want 10 posts in category.php . let suppose i am clicking one category in categories so it should show result 10 post of each category. i will appreciate if you can let me know Thnaks – umair May 5 '14 at 12:19
0

You can use pre_get_posts and is_category conditional tags to set posts_per_page to 10 for category pages.

Here is a working exacmple

function wpse_posts_cat_pages( $query ) {
    if ( $query->is_category() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', '10' );
    }
}
add_action( 'pre_get_posts', 'wpse_posts_cat_pages' );

What this code do:

  • Check if it is the main query $query->is_main_query() and if the page is a category (category.php) page $query->is_category(), and if both of these are valid, the amount of posts are limited to 10, no matter what is set in the back end
  • Excellent. As my answer solved your problem, would you please credit me by accepting my answer by clicking on the checkmark next to my answer just below the up/down arrows. This will make sure that my answer not only helps you, but others as well. – Pieter Goosen May 6 '14 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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