I created two template pages: galerie.php and homepage.php.

Galerie already displays posts. So Homepage is homepage.php and posts page is galerie.php.

Now the problem is that I wanted to display a post from Category category-test into homepage.php. I used the following code to try to display the posts from category-test:

<?php $posts = get_posts('category_name=' . 'category-test'); foreach($posts as $post) { ?>
      <?php the_title(); ?>
      <p><?php the_content(); ?></p>
 <?php } ?>

But for some reason this only displays the post title. It doesn't display the body content of the post.

What is the problem with it?

  • You have to use setup_postdata( $post ); inside your foreach loop just before calling the_title(). This will make the template tags available – Pieter Goosen Nov 17 '14 at 16:30
  • How? Could you please add it into my code? – azhpo Nov 17 '14 at 16:32
  • Done. Just for interest sake, feel free to take a tour to learn how the site operates. :-) – Pieter Goosen Nov 17 '14 at 16:45
  • Thank you. I come from Stack Overflow. It's pretty much the same. I know how it works. :) – azhpo Nov 18 '14 at 7:34

The template tags (like the_content()) aren't available when using get_posts. In order to make the template tags available, you have to make use of setup_postdata( $post );


$posts = get_posts('category_name=category-test'); 
foreach($posts as $post) { 
    setup_postdata( $post );

    the_title(); ?>
    <p><?php the_content(); ?></p>
<?php } ?>
  • Thank you. Worked just fine. :) Btw am I doing this right? I mean, I would like to know if this is the right way of doing what I want. And I would like to understand why did my code displayed the the_title() of the post but not the_content() – azhpo Nov 18 '14 at 7:27
  • Is homepage.php a static front page. If yes, then you are you doing it correctly. Remember, you can use get_posts or WP_Query for this. I tend to prefer WP_Query as the template tags are available by default, and it is easier to work with if you need paginated queries. – Pieter Goosen Nov 18 '14 at 8:06
  • Please see this post, this should answer a few questions for you – Pieter Goosen Nov 18 '14 at 8:11

I would use WP_Query with any of the category parameters as its more flexible.

You can add it directly to your template or create a template tag which you can then add in any file or custom function.


// The Query
$the_query = new WP_Query( 'cat=4' );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        echo '<li>' . get_the_title() . '</li>';
    echo '</ul>';
} else {
    // no posts found
/* Restore original Post Data */

Why don't you use WP_Query? [Modified code, used WP_Query instead of query_posts as suggested]

<?php $arg = array(
    'category_name' =>  'category-test'
$newQ = QP_Query($arg);
while($newQ->have_posts()) : $newQ->the_post();?>
    <?php the_title(); ?>
    <p><?php the_content(); ?></p>
<?php endwhile;
  • 1
    Never use query_posts. It is just a function prone to fail unexpectitely – Pieter Goosen Nov 17 '14 at 19:14
  • Never use query_posts as its highly inefficient. In this case, WP_Query is the best function. – Brad Dalton Nov 18 '14 at 2:20

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.