简体   繁体   中英

Getting WP excerpt in a category.php outside of WP loop

I need some help pulling post excerpt out of the loop. I have few 'li's and need to display excerpt while hovering them. If it was inside the loop it would no problem, but haw can I force WP to display an excerpt form a post's link currently hovered?

<ul>
WP loop BLAH BLAH
<li>(number of <li>s depending of number of posts in current category)</li>
end of loop
</ul>
<div>show excerpt from currently hovered li</div>

thanks in advance!

PS: I tried with get_the_excerpt function, but it displays only last post's excerpt..

When you are in the wp loop, the current post counter is incremented in every iteration. and the get_the_exceprt function will display the excerpt of the current post.

so when the loop is over and you get out of it, and you call the get_the_excerpt function, it will display the 'last post's excerpt as for it it's the current item.

solution:

you need to reset the loop using rewind_posts() function before calling the get_the_excerpt function in new loop.

 while ( have_posts() ) : the_post();
//....loop.
endwhile;

rewind_posts();

//start a new loop.
 while ( have_posts() ) : the_post();
$excerpt=get_the_excerpt();

endwhile;

or while you are in the first loop, store the excerpts in an array, and use after you get out of the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM