WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Page template not finding have_posts() data (6 posts)

  1. Matt_Byrne
    Member
    Posted 6 years ago #

    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:
    http://blog.wearetheoriginals.co.uk/

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

    http://blog.wearetheoriginals.co.uk/?page_id=9

    What am I doing wrong?

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

    <?php
    /*
    Template Name: services
    */
    ?>
    <html>
    <head>
    <title>Services</title>
    </head>
    <body>
    
    	<?php 
    
    	$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);
    
    					}else{
    
    						for ($i =0; $i < count($clientArray); $i ++) {
    
    							if($clientArray[$i]['Client']==$clientName){
    								$clientArray[$i][$category]= 'yes';
    							}
    						}
    					};
    				endif;
    		endwhile;
    	endif;
    
    	print_r($clientArray);
    	?><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
    	}?></table>
    </body>
    </html>
  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    ...
    if (in_category(4))
    ...

    Pages are not in categories.

  3. Matt_Byrne
    Member
    Posted 6 years ago #

    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 http://blog.wearetheoriginals.co.uk/?cat=4 I can see all the posts I want to include.

  4. Matt_Byrne
    Member
    Posted 6 years ago #

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

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 6 years ago #

    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:
    http://codex.wordpress.org/Theme_Development
    http://codex.wordpress.org/Templates
    http://codex.wordpress.org/Stepping_Into_Templates
    http://codex.wordpress.org/Template_Hierarchy

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

  6. Matt_Byrne
    Member
    Posted 6 years ago #

    Otto,

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic