Support » Fixing WordPress » How to add an IF statement within query_posts

  • Hi,

    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    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' );

    Hi keesiemeijer,

    Let me try this and get back to you!

    Many thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to add an IF statement within query_posts’ is closed to new replies.