0

Using WordPress 3.7.1 I am trying to display all Regular Post on my created page lest say TestPage. Here are the steps I took to do this:

1- Generate a Custom Page Template called:Test Page and loaded by following code
2- Generate a Page Called TestPage based on Test Page Template

after updating the page I am not getting any of Post on the page while I have already generated some!

<?php
/*
Template Name: Test Page
*/
?>
<?php get_header(); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <h1><?php the_title() ;?></h1>  
    <?php the_content(); ?>
<?php endwhile; else: ?>
    <p>Sorry, this page does not exist</p>
<?php endif; ?>

<?php get_footer(); ?>

he abouve code actually is loading the page whit title and content of the TestPage and not by Posts!Can you please let me know why this is happening?

1
  • Just to be sure the basics are covered, have you selected your Test Page as your Posts Page in Settings > Reading? – sabreuse Nov 3 '13 at 20:35
1

Can you please let me know why this is happening?

A loop like yours assumes the data from the "main query". You've created a custom page template so the main query on that page is going to be the single "TestPage" data. That is the way it is supposed to work. That is, if ( have_posts() ) : while ( have_posts() ) : the_post(); doesn't always give you the post archive data.

To get the posts you'd need to create a new query and loop over that. Like this:

$newq = new WP_Query(array('post_type'=>'post'));
if ($newq->have_posts()) {
  while ($newq->have_posts()) {
    $newq->the_post();
    the_title();
  }
}

You should probably look over the Template Hierarchy carefully, because this may not be the way you want to go about things at all.

4
  • Hi s_ha_dum, Thanks for you perfect explanation , I got it now – Behseini Nov 2 '13 at 23:17
  • My code is more an example than a finished piece of code. You will have to take it and alter it to fit your needs. – s_ha_dum Nov 2 '13 at 23:20
  • Can you please take alook at this link and let me know how to fix this issue this? wordpress.stackexchange.com/questions/120882/… – Behseini Nov 3 '13 at 0:21
  • I already Google this but couldn't find any solutions! – Behseini Nov 3 '13 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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