0

Just as the title says this code is not pulling the excerpt from $recent like it should be, instead it is pulling it from the current page.

   <?php
$args = array( 'post_status' => 'pending');
$recent_posts = new WP_Query( $args );

foreach( $recent_posts as $recent ){
$recent->the_post(); // Add this here to cause the other functions to work without the post ID
    echo '<div id="votes"><li id="voteimage"><a href="' . get_permalink() . '" title="' . esc_attr( $recent["post_title"] ) . '">';
    echo get_the_post_thumbnail($recent["ID"], 'thumbnail');
    echo '</li></a>';
    echo '<li class="vote-title"><a href="' . get_permalink() . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li>';
    echo '<li class="vote-desc"><a href="' . get_permalink() . '">' . get_the_excerpt() . '</a> </li>';
    echo '</div><br></br>';
}

?>

get_the_excerpt($recent["ID"]) is the part pulling the excerpts, tried using the_excerpts and swapping it around to ($recent[get_the_excerpt]) too, nothing I try seems to work.

Example: streakingpirates.com

1
  • Inside your loop, add $recent->the_post() Commented May 20, 2014 at 13:59

1 Answer 1

0

In the docs, the parameter to pass the post ID is marked deprecated.

That means, in order for your custom loop to work, you need to add $recent->the_post()

However, as you pointed out, you can't do that when you use wp_get_posts, so I'd suggest modifying the query to be a custom loop:

$args = array( 'post_status' => 'pending');
$recent_posts = new WP_Query( $args );

Then, you should be able to use your code:

See below:

while ( $recent_posts->have_posts()) {
    $recent_posts->the_post(); // Add this here to cause the other functions to work without the post ID
    echo '<div id="votes"><li id="voteimage"><a href="' . get_permalink() . '" title="' . esc_attr( $recent["post_title"] ) . '">';
    echo get_the_post_thumbnail($recent["ID"], 'thumbnail');
    echo '</li></a>';
    echo '<li class="vote-title"><a href="' . get_permalink() . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li>';
    echo '<li class="vote-desc"><a href="' . get_permalink() . '">' . get_the_excerpt() . '</a> </li>';
    echo '</div><br></br>';
}

Also note that get_the_excerpt() will return un-filtered content. You may want to wrap it in an apply_filters() like so:

apply_filters('the_content', get_the_excerpt());
5
  • Sadly that's causing an error message on the same line I placed it streakingpirates.com/pending that's the error Commented May 20, 2014 at 14:10
  • @PirateScott - shoot. I was worried about that. Thankfully, that should be a simple fix - check my edits in a moment. Commented May 20, 2014 at 14:13
  • Error is still there, my new code is now up in the top. I should say my knowledge of PHP and the wordpress functions is lacking, I only started with both properly 2 days ago. Commented May 20, 2014 at 14:24
  • @PirateScott - OK, time to make the "Full swap" of code to properly use the WP_Query. Please see my edits in a moment... thanks for your patience! Commented May 20, 2014 at 14:46
  • You are a star, thanks. It worked but the title doesnt show now, I think I know what the problem to that is though Commented May 20, 2014 at 14:54

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.