Support » Themes and Templates » Page template not finding have_posts() data

  • Resolved Matt_Byrne


    Hi there,

    I’m very new to WordPress but very impressed. However, I’m stuck on a problem.

    I’m trying to build a custom page called services that collects information from post categories + custom fields, and generates a neat little table.

    The following code (minus the template name) works just fine if I edit the index.php template:

    But when I create a new page (not a new entry) and assign a template to it, with the code below. I get zip:

    What am I doing wrong?

    BTW I know the code is probably horrible, so be kind. 😉

    Template Name: services
    	$clientArray = array();
    	$clientNameArray = array();
    	$blank = array('Client' =>'name', 'website' => 'no', 'logo' => 'no', 'stationery' => 'no');
    	if (have_posts()) :
    		while (have_posts()) : the_post();
    				echo 'foo';
    				$tempArray = $blank;
    				if (in_category(4)) :
    					$category =get_the_category();
    					$category = $category[0]->cat_name;
    					$clientName = get_post_meta($post->ID, 'client', true);
    					if(!array_search($clientName, $clientNameArray )){
    						array_push($clientNameArray, $clientName);
    						$tempArray['Client'] = $clientName;
    						$tempArray[$category] = 'yes';
    						array_push($clientArray, $tempArray);
    						for ($i =0; $i < count($clientArray); $i ++) {
    								$clientArray[$i][$category]= 'yes';
    	?><table border='1'><?php
    	for ($j =0; $j < count($clientArray); $j ++) {
    	?><tr> <?php
    		foreach ($clientArray[$j] as $key => $value){
    		?><td><?php echo $value; ?></td><?php
    	?><tr /><?php
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Samuel Wood (Otto)

    (@otto42) Admin

    if (in_category(4))

    Pages are not in categories.

    Hi there Otto,

    Thanks for getting back to me so quickly.

    I’m looking to filter the posts with the if (in_category(4)). Any that are in this category should be included in the final output.

    If I use I can see all the posts I want to include.

    Do you know why the index.php version works and my page template dosen’t?

    Moderator Samuel Wood (Otto)

    (@otto42) Admin

    Errr… I don’t quite understand your question. Maybe I should explain the Loop a little bit.

    The Loop is on *all* pages and posts and everything else in the theme. It is the main display function. It will display the post or page content. Got it? Okay..

    When you do ?cat=4, then you’re telling it to load the Posts in category 4, using otherwise default settings. It does so, then loads up the correct template (it looks in the theme for category-4.php, then category.php, then archive.php, and finally index.php, and uses the first one it finds), and then runs the template. The template has a loop in it, which displays the posts that got loaded.

    But when you are looking at a PAGE, like ?page_id=6, then WordPress goes out and retrieves the content of that page. That content is loaded and when the Loop runs, it will display that content.

    It doesn’t get any Posts, because you didn’t tell it to get Posts, you told it to get a Page. Pages are just another form of Posts to WordPress.

    Make more sense now? The index.php is being used on the ?cat=4 because it falls back to the default. The custom template you made can be exactly the same, but still display something different, because you’re loading different content.

    Also, you don’t want to display Posts on a Page. That’s not what it is designed for, and not the best way to do it. If you want to display posts from category 4, then by all means, use the ?cat=4 URL instead. If you want that particular page to look different, then don’t make a Page Template. Make a Category template. Just copy the index.php to category-4.php and adjust that file as you see fit.

    And please, read these:

    There’s dozens more. Themes and templates are simple once you understand what’s going on.


    You’re a star. The URL version works!

    Thank you so much for your input.

    I’ve only been using WP for three days now (I’ve migrated over from Movable Type) and it’s been a complete joy to use.

    Pat yourself on the back, Otto rules!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Page template not finding have_posts() data’ is closed to new replies.