0

I have a loop where I display all posts on the site, the problem is that the_excerpt is not being displayed. What am I doing wrong?

  <?php 
    $all_posts = get_posts(array('numberposts' => -1));
    $total_posts = count($all_posts);
    $posts_per_column = intval($total_posts / 3);
    $count = 0;
    $col = 1;
    foreach($all_posts as $post):
  ?>

  <!--post-->
  <div class='post' id='article<?php echo $count; ?>'>
    <div class='thumb'> 
      <a href='<?php the_permalink() ?>'>
      <!--todo: round thumb images and make them of variable height-->
      <?php the_post_thumbnail() ?>
      <?php 
        $cat = get_the_category(); 
        $cat_long = $cat[0]->cat_name;
        $cat_split = explode(' ', $cat_long); 
        $cat_short = strtolower($cat_split[0]);
      ?>
      </a>
      <span class='tag <?php echo $cat_short ?>'><?php echo $cat_long ?></span>
    </div>

    <div class='inner <?php echo $cat_short ?>'>
      <h2 class='entry-title'><a href='<?php the_permalink() ?>'><?php the_title() ?></a></h2>
    </div>
    <div class='entry-content'>
      <?php the_excerpt() ?>
    </div>
  </div>
  <!--end post-->

2 Answers 2

3

You need to be using setup_postdata

foreach($all_posts as $post):
    setup_postdata($post);
    // the rest of your loop

If you look at the docs for get_posts you will see that in all of the examples.

1
  • Super, this did it!
    – Lorenzo
    Commented Mar 24, 2013 at 17:23
0

Change this:

<?php 
    $all_posts = get_posts(array('numberposts' => -1));
    $total_posts = count($all_posts);
    $posts_per_column = intval($total_posts / 3);
    $count = 0;
    $col = 1;
    foreach($all_posts as $post):
  ?>

to this:

<?php
    $all_posts = new WP_Query(array('posts_per_page' => -1));
    $total_posts = $all_posts->found_posts;
    $posts_per_column = intval($total_posts / 3);
    $count = 0;
    $col = 1;

    if($all_posts->have_posts()):
        while($all_posts->have_posts()):
            $all_posts->the_post();
?>

and at the end, you have somewhere endforeach. Remove it and put:

<?php
        endwhile;
    endif;

    wp_reset_postdata();
?>

That's the right way (in my opinion) how it is done - therefore you can't reach the bug with the $post variable like it's in your case. In your case, It's look like you just overrode global $post variable - I'm wondering how some of your "the_" tags worked and some didn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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