简体   繁体   中英

controlling the number of posts on a wordpress archive page

I have set my wordpress blog's frontpage to show only three posts. On the archive.php template, when the posts of a tag are viewed, I want to show 10 results.

How do I do this?

I tried this php code. But instead of only showing the posts with a certain tag, it queries all recent posts.

//in archive.php (before the loop)
query_posts('posts_per_page=10');

Just append the query with a tag parameter (as shown here: http://codex.wordpress.org/Function_Reference/query_posts#Tag_Parameters ):

query_posts('posts_per_page=10&tag=your_desired_tag');

EDIT : If you use this within a function you can also just append your limit to the original query like this:

function my_archive_loop($content) {
    global $query_string;
    query_posts($query_string . "&posts_per_page=10"); 
}

The variable $query_string should than include all the default parameters like the current tag, category, year or whatever archive page you are viewing.

can you try this:

  function post_per_page_control( $query ) {
     if ( is_admin() || ! $query->is_main_query() )
       return;

    // For archive.You can omit this
     if ( is_archive() ) {
          //control the numbers of post displayed/listed (eg here 10)
          $query->set( 'posts_per_page', 10 );
          return;
     }

     // For your tag
     if ( is_tag() ) {
          //control the numbers of post displayed/listed (eg here 10)
          $query->set( 'posts_per_page', 10 );
          return;
     }
  }
  add_action( 'pre_get_posts', 'post_per_page_control' );

read more here:

1 http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

2 http://codex.wordpress.org/Function_Reference/query_posts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM