Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to show post excerpt as in navigation. I'm trying this in plugin, does it make any sense? i don't know.

I used $nex_obj = get_adjacent_post(false, '', true); for next post and it returns post object when i try to show the post excerpt using $nex_obj->post_title as in. This doesn't work out.

So i tried

echo $excerpt = apply_filters('get_the_excerpt', $nex_obj ->post_excerpt); but this throws error Fatal error: Maximum function nesting level of '100' reached, aborting! Is this correct or wrong? I'm trying this in localhost.

So finally i tried loop to get the excerpt based on the id

Default loop

$id=$nex_obj->ID;

    if (have_posts()) :
       while (have_posts()) :
    if($id==get_the_ID()){
        the_excerpt();
    }
       endwhile;
    endif;

The above loop keep on running the browser and making it unresponsive. I think i'm doing this in wrong way, i heard i shouldn't use default loop. So skipped this and tried another query loop without any argument as i don't need any filter on particular post

     query_posts($args);
    // The Loop
$id=$nex_obj->ID;
        while (have_posts()) : the_post();
         if ($id== get_the_ID()) {

     echo $excerpt = get_the_excerpt();

            }
    endwhile;

    // Reset Query
     wp_reset_query();

How to get excerpt of post with post ID or something ? All i have is post Object

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted
$nex_obj = get_adjacent_post(false, '', true);

If there is an adjacent (previous) post, this should work. You should be able to get the excerpt with:

echo $nex_obj->post_excerpt;

However, what you will get is the manually created excerpt. If you are expecting the automatically created excerpt you won't get it. That is created on display from the post content, and not saved to the database. You can use...

echo wp_trim_words($nex_obj->post_content);

... to simulate the effect.

This:

echo $excerpt = apply_filters('get_the_excerpt', $nex_obj ->post_excerpt); 

Won't give you the automatic excerpt either, because you need to be passing the post body not the manually generated excerpt, but I don't know why you get the error. I don't get that error when I try it.

This:

if (have_posts()) :
   while (have_posts()) :
   if($nex_obj==get_the_ID()){
      the_excerpt();
   }
   endwhile;
endif;

Is close but broken. The the_post() function is what increments the counter. Without it, the Loop will process the same post over and over.

This one:

query_posts($args);
// The Loop
while (have_posts()) : the_post();
     if ($nex_obj == get_the_ID()) {

         echo $excerpt = get_the_excerpt();

     }
endwhile;

// Reset Query
wp_reset_query();

Is confusing. Your object will never match the post ID-- an int is not an object, so I am not sure why you have that if. It should probably work without the if but...

Please don't use query_posts.

It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.

http://codex.wordpress.org/Function_Reference/query_posts (emphasis mine)

share|improve this answer
    
Thanks for the answer. I missed the post id in condition inside the loop. Check my edits in loop –  sun Jan 30 at 6:43
    
wp_trim_words() is working. echo $nex_obj->post_excerpt; is not working.It shows nothing, its a string with length=0 when i var_dump. I'm accepting the above answer, it'll be helpful if i know why it is not working in echo $nex_obj->post_excerpt; and in my loop logic. I have updated the loop please check –  sun Jan 30 at 6:58
    
I am pretty sure I have already answered those questions. –  s_ha_dum Jan 30 at 14:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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