How to add an IF statement within query_posts (3 posts)

  1. sixfootjames
    Posted 4 years ago #


    I was wondering how I might be able to add and IF statement with query_posts please? Something like...

    from this...

    <?php query_posts('category_name=home-news-events-title&posts_per_page=1');

    to this...

    <?php query_posts('category_name=home-news-events-title || category_name=sports_title&posts_per_page=1');

    Many thanks.

  2. keesiemeijer
    Posted 4 years ago #

    If you want only 1 post per page on the category pages "home-news-events-title" and sports_title you can try it with this. Remove the query_posts() from the category template and put this in your theme's functions.php:

    function my_post_queries( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_category('home-news-events-title') || is_category('sports_title')){
          $query->set('posts_per_page', 1);
    add_action( 'pre_get_posts', 'my_post_queries' );
  3. sixfootjames
    Posted 4 years ago #

    Hi keesiemeijer,

    Let me try this and get back to you!

    Many thanks

Topic Closed

This topic has been closed to new replies.

About this Topic