4

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?

2
  • did you omit the first few lines with the page template name in your code on purpose? what is the file name of the page template? what is the name of the page? have you excluded a possible naming conflict?
    – Michael
    Commented Jul 9, 2011 at 21:35
  • @Michael Thanks for pointing that out. I have <?php /* Template Name: Teams */ ?>in the first few lines. The file name of the page template is teams.php. I have tried renaming the file as well as the name of the template in the first few lines but no dice...
    – SamY
    Commented Jul 9, 2011 at 22:05

2 Answers 2

2

My issue was research was not using page-research.php but instead using archive.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

1

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:

 <?php get_header();?>

        <section id="content">

            <?php
                $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_reset_query();
            ?>
        </section>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>

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:

$custom_query_1 = new WP_Query();
$custom_query_1->query('post_type=teams&posts_per_page=7');

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.