1

I am new in WordPress plugin development. I am developing a simple WordPress plugin in which get_the_excerpt() not working in the loop. Other tags such as get_the_title(), get_the_permalink() even get_the_content() working fine. Please somebody help me out on this. Here is the code

if($loop->have_posts()) : 
while($loop->have_posts()) : 
      $loop->the_post(); 
   $content .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a><br />'.get_the_excerpt().'</li>';
endwhile; wp_reset_postdata(); endif; 
2
  • Can you clarify "not working"? I assume you just get no output from get_the_excerpt()? Do you use the more tag in your posts? Commented Apr 30, 2014 at 16:11
  • @TheDeadMedic thanks again. Yes, you are right... when I add get_the_excerpt(), post pages stop loading. And yes I used more tag in my posts.
    – Babu
    Commented Apr 30, 2014 at 17:29

1 Answer 1

2

Unlike the_excerpt(), which auto-generates an excerpt from the content if one does not exist, get_the_excerpt() merely returns the contents of the post_excerpt field.

Instead, use apply_filters( 'the_excerpt', get_the_excerpt() ), which will trigger the same processing as the_excerpt(), but return the output rather than echo it.

7
  • Are you sure? According to the codex, both functions generated the same string but the_excerpt() directly display the string and adds opening and closing <p> tags around it.
    – cybmeta
    Commented Apr 30, 2014 at 13:04
  • 3
    Pretty sure. If you dive into wp-includes/default-filters.php, they're all attached to the_excerpt filter, but not get_the_excerpt. Commented Apr 30, 2014 at 13:18
  • This answer is correct. Commented Apr 30, 2014 at 13:48
  • 1
    I think you are both wrong. get_the_excerpt() returns the contents of the post_excerpt field and if is empty, then it auto-generates one by applying wp_trim_excerpt to the post content. the_excerpt() adds opening and closing <p> tags to the string generates by get_the_excerpt() and echoes it. See wp-include/post-template.php. So, both functions generates the same excerpt string (except for <p> tags). So, your answer doesn't respond to the Babu's question about why get_the_excerpt() is not working inside the loop.
    – cybmeta
    Commented Apr 30, 2014 at 14:13
  • @TheDeadMedic I tried to use apply_filters( 'the_excerpt', get_the_excerpt() ) but it didn't work. thanks TheDeadMedic and cybnet for responding. Could you guys please tell me how I can fix it?
    – Babu
    Commented Apr 30, 2014 at 15:33

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.