I have a template set up and I’ve created a page in WordPress and have chosen the corresponding template in the drop down. I have created templates for several pages of this site and they’ve all worked fine; however, this one template is causing problems. WordPress keeps fetching the archive page for some reason. The exact body class is this: archive post-type-archive post-type-archive-teams logged-in
Here is the template I’ve created.
<?php get_header();?>
<section id="content">
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('post_type=teams&posts_per_page=7');
while ( $wp_query->have_posts() ) : $wp_query->the_post();
?>
<article class="post team" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<section class="entry">
content here
</section>
</article>
<?php
endwhile;
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Can someone help me out in figuring out why this isn’t working?
My issue was
research
was not usingpage-research.php
but instead usingarchive.php
I resolved it not by the “Static Pages and Custom Post Types cannot have the same slug” solution, but on the dashboard in Settings -> Permalinks.
It was on “Day and name” and I changed it to “Numeric” and the research page loaded from
page-research.php
The most basic approach to this is to do what I do: when building template pages I precede them with “template_”.
Thus if you desired a team.php template for a page you would look for template_team.php.
Additionally, you have code on your page you don’t actually need.
I would change your code to simply be:
And the reason I would make the changes is because you are not actually using $temp so there is no reason to assign it. Additionally you don’t have any reference to a wp_query when the page loads and even if you did you don’t use it anywhere in the page.
You instantiate a whole new query (using the NEW keyword) and, so far as the page is concerned, that is the only query you have on the page at all. Thus, that is the only query you need to reset.
What is actually happening is you click on a link and that directs you to the page “teams”. This loads the “template_teams.php” and does have all the variables assigned to $post for that page as soon as the page loads. Within $post is the actual content of the page you have created in the WordPress text editor and assigned the template to.
Now, if you need a different query on top of the default one loaded with the page you could easily change your variable of $wp_query to something more relevent to what you want:
Then you would be able to use the default query AND your custom query anywhere in the page at any time. Using the clean up function wp_reset_query() is ideal to use after each loop BUT is clears all queries.
To avoid this from clearing any custom queries you may have simply place the new query only where you need it.