WordPress.org

Ready to get started?Download WordPress

Forums

Is this expected is_home() behavior? (3 posts)

  1. VFHwebdev
    Member
    Posted 5 years ago #

    In the following code:
    query_posts( 'showposts=5&cat=4' );
    if (have_posts()) { while (have_posts()) { the_post();
    if(is_home()){
    ////do some stuff
    }
    if(is_category()){
    /////do some other stuff
    }

    When on my home page, is_home returns false and is_category returns true.

    If I remove the &cat=4 from query_posts things behave as normal. Of course then I get all the posts instead of just the posts from category 4 like I want.

    Is this expected behavior or a bug?

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 5 years ago #

    Expected behavior. When you use query_posts, you are modifying the default query, and that affects those template tags.

    If this is not your main page's Loop, then you should not use query_posts. You should create a new WP_Query object and use that instead, as a separate query won't modify those values.

  3. flashyflashy
    Member
    Posted 5 years ago #

    Are you trying to show only the posts from particular category on your main page?. If so why not just use

    query_posts( 'showposts=5&cat=4' );
    if (have_posts()) { while (have_posts()) { the_post();

    ////do some stuff

    }

    in your index.php page.

    If your theme has single page (index.php) taking care of both category and mainpage, then
    1.Upload category.php (same file as index.php but renamed to category.php) to example.com/wp-content/themes/[theme-name]/ .

    2.or try this instead,
    if(is_home()){
    query_posts( 'showposts=5&cat=4' );
    if (have_posts()) { while (have_posts()) { the_post(); //****** }
    }
    if(is_category()){
    /////do some other stuff
    }

Topic Closed

This topic has been closed to new replies.

About this Topic