• I’m creating a custom Page template and have run into an apparent interaction between the functions query_posts() and is_page() that I can’t seem to resolve. Haven’t been able to find posts with a similar problem, so here’s a description:

    In my custom Page template, I call query_posts() to set up a loop. I want to grab the first 50 posts and display them.

    Code, Figure A:
    <?php query_posts(‘showposts=50’); ?>
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    (*snip* the rest of the loop displays the titles of the posts. it’s a valid loop and works, so I’m leaving that out.)

    After I finish this loop, sometime later, I call is_page() from the sidebar.php file. I’m customizing the sidebar based on whether or not we’re in a Page.

    Code, Figure B:
    <?php if ( !is_page() ) { ?>
    (we are not in a Page, display x)
    <?php } else { ?>
    (we are in a Page, display y)
    <?php } ?>

    Now, the strange thing is that the code in Figure B works great, so long as the line:
    <?php query_posts(‘showposts=50’); ?>
    in Figure A does not exist! If I take the line out, it works, if I put it back in, it doesn’t work and (!is_page()) always equals TRUE, even when we’re in a Page.

    Does the use of query_posts() change the functionality of is_page() or break it in some way that isn’t documented? Any insight would be much appreciated… I’m stumped.

    Thanks…

Viewing 2 replies - 1 through 2 (of 2 total)
  • I think you’re on to something here, i’m experiencing the same thing. is_page or is_home is completely ignored as is query_posts when they are both used on a page.

    By having a “query_posts” in the template, you override the original, behind-the-scene “query_posts” which sets “is_page” as true.

    You could restore the condition “is_page” back to true by adding the following after your custom “query_posts” and its associated loop.

    // custom query_posts
    // the Loop

    $wp_query->is_page = true;
    if( is_page() ) {
    // code in this block will be executed
    }

    Note that the above is unofficial solution, which means that it may not be compatible with future version of WP.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Page Template, query_posts() breaks is_page()?’ is closed to new replies.