To display a custom page template you usually rewrite the default WordPress query that is used to read the post / page content and display it: $wp_query.

By doing so, you end up somehow breaking it’s flow and don’t let it finish correctly (this is not what happens, you really break it though).

This, for instance, prevents the query to correctly set the current-menu-item and other css classes to the menu items that should be marked as selected when visiting a page with a custom template inside.

Let’s see where it goes wrong.

You start by rewriting the default WP query and storing the old one in a variable. The query_posts() function is a filter. You can check the parameters and how it works by checking the codex.

<?php $temp_query = $wp_query; ?>
<?php query_posts('category_name=events'); ?>

You then loop through the posts and display the titles linking to them (or whatever you’d like to display).

<?php while (have_posts()) : the_post(); ?>
<div class="post" id="post-<?php the_ID(); ?>">
	      <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>

The problem happens here. If you search the web looking for how to include posts inside a page people usually forget to add the following code after their snippet. It basically restores the original WP query and let’s it “run fully”, updating the css classes etc.

All you have to do is add this piece of code after <?php endwhile; ?> and you’re good to go.

<?php $wp_query = $temp_query; ?>

If it still doesn’t work for you feel free to leave a comment and I’ll look into your problem.


Leave a Reply

Your email address will not be published. Required fields are marked *