• Resolved mjm22178

    (@mjm22178)


    In 2.0, I could take the post object returned by get_results() and call the_excerpt() on that object before the main loop started.

    After upgraded to 2.1, this no longer seems to work (doesn’t return any text). However, calls to the_permalink(), the_title(), and the_time() still work in this scenario. How can I get the_excerpt() to work before the main loop?

    An example of what I’m trying to do is:

    <?php
    $random_posts = $wpdb->get_results($sql);
    foreach ($random_posts as $post) {
    ?>
    <a href="<?php the_permalink(); ?>"
    title="<?php the_title(); ?>"><?php the_title();?></a><br/>
    Posted on <?php the_time('j F Y', display); ?>
    <?php the_excerpt(); ?>
    <?php
    }
    ?>
Viewing 1 replies (of 1 total)
  • Thread Starter mjm22178

    (@mjm22178)

    I finally figured this out, though I’m not sure why the old code worked with 2.0 and not 2.1. The answer is to call the function setup_postdata($post) immediately after the foreach. So, the final, working result is:

    <?php
    $random_posts = $wpdb->get_results($sql);
    foreach ($random_posts as $post) {
    setup_postdata($post);
    ?>
    <a href="<?php the_permalink(); ?>"
    title="<?php the_title(); ?>"><?php the_title();?></a><br/>
    Posted on <?php the_time('j F Y', display); ?>
    <?php the_excerpt(); ?>
    <?php
    }
    ?>
Viewing 1 replies (of 1 total)
  • The topic ‘Using the_excerpt() before the main loop in 2.1’ is closed to new replies.