query_posts() and get_posts() eliminate conditional tags?
-
I was looking for a way to display page specific content in the sidebar. I had the problem that placing query_posts() or get_posts() before the loop in my page templates it breaks the execution of conditional tags like is_page(‘8’) in the sidebar template:
THE FOLLOWING SCENARIO DOES NOT WORK:
(values of page-id, page-title or page-slug are all ZERO in sidebar.php)Code in my-page-template.php
<?php query_posts("cat=any_cat_id"); ?> <?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?> My post listing. <?php endwhile; ?><?php else : ?>No posts.<?php endif; ?>
Code in sidebar.php
<?php if (is_page('any_page_id')){ ?> Content in sidebar for pageID=8 <?php } ?>
THE FOLLOWING SCENARIO WORKS:
(..in my case. The solution was to work with sessions)Code in my-page-template.php
<?php //this line before ANY query_posts() or get_posts() queries and loopings! $page_id = $wp_query->get_queried_object_id(); $_SESSION["pageid"] = $page_id; ?> ... query_posts() and loop
Code in sidebar.php
if ($_SESSION["pageid"] == any_page_id){ echo "This is page with ID=any_page_id";}
Question:
Can a moderator or other experienced users of this forum confirm that using query_posts() or get_posts() in a page template breaks conditional tags in the sidebar with WP3.2.X ? What do I need to consider when using the working code above in terms of restrictions?
- The topic ‘query_posts() and get_posts() eliminate conditional tags?’ is closed to new replies.