dorich
dorich

Reputation: 1089

Wordpress Template for Custom Post Type

Editor Note: original title of this Question - "Wordpress Template Changes Based on Template Code"

I have a page that reads the variable ( a single letter) from the url. The page then displays custom posts assigned to that letter (the letter is a custom field).

The problem is that the template assigned to the page is not doing the final rendering of the page.
It contains the following code:

<?php 
    $letter = $_GET['letter'];

    /*If there is no value returned then the loop defaults to searching for items beginning with 'A'*/ 
    if ( $letter=="" ) 
        $letter = 'A';

    /* The following <h1> needs to be outside of the loop */
?>
    <h1>Glossary Items Listed Under Letter "<?php echo $letter ?>"</h1> 

<?php 
    $loop = new WP_Query( 
                    array( 
                        'post_type' => 'glossary', 
                        'meta_key'=> 'first-letter', 
                        'meta_value' => $letter, 
                        'posts_per_page' => 10 ) 
                    ); 
?>

<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="glossarybody"> 
        <h2><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
    </div>
<?php endwhile; ?>

When that code is on the page the page displays the correct results ( a list of titles) but using the plugin Reveal Templates I see that the page is being rendered with page.php.
The correct template should be glossary-terms.php (I've tried using the filename terms.php in case the word glossary, the name of the custom post type involved, was causing a problem).

If I remove the above code and so that I'm left with a more or less static page that has some html, header, footer and sidebar, then the page renders using the correct template.
This suggests that something in the operation of Wordpress is causing the template to change in the execution of the intial template.

I'd appreciate any pointers on what might be wrong with my setup and how to get the correct template to render the page.

UPDATE: I discovered that if I change the calling link I can get the correct template to render the page. For instance if the calling link ended in ".../terms?letter=A" and I changed it to "..../terms?letter=A/" then the page is rendered with the correct template.

UPDATE: regarding the previous comment. if I change the content of the field and the calling link to something other than the designated letter, say xya, then the whole process works correctly. So it would appear that something associated with this custom field is causing the designed template to be ignored.

Upvotes: 0

Views: 703

Answers (1)

dorich
dorich

Reputation: 1089

There was not a problem with the function of Wordpress.

The problem arose because I was using a plugin to show the template being used and it appears that the plugin was malfunctioning. Looking at the body tag in the page source shows that the correct template is rendering the page.

Upvotes: 0

Related Questions