0

I'm using a modified version of the code from a question (Get WordPress Posts Grouped by Month) to group my posts by year.

Everything is working fine, but I'm having trouble getting the post excerpt to display for the posts. Here is my current code: Pastebin

I was able to get the post content to display by using post_content in place of post_excerpt in both the query and the output, but the excerpt just doesn't want to show up. I've also tried just using the_excerpt(); and other methods of retrieving the excerpt, with no luck.

I appreciate any help.

2

I guess your post_excerpt field is empty and you need to generate an excerpt from the post_content.

I agree with @t f using the general WP_Query(), have_post(), the_post(), the_excerpt() method of looping.

I just want to mention that WordPress comes with the handy wp_trim_words() function that can be used to shorten text strings.

Outside the loop, one can also make a custom version of wp_trim_excerpt():

function my_trim_excerpt( $text, $length = 55, $more = ' […]'  )
{
     $text = strip_shortcodes( $text );
     $text = apply_filters( 'the_content', $text );
     $text = str_replace(']]>', ']]>', $text);

     $excerpt = wp_trim_words( $text, $length, $more );
     return $excerpt;
}

where it can be use it like this:

echo my_trim_excerpt( $mylongtext );

or

echo my_trim_excerpt( $mylongtext, 40, ' […]' );

So if your text is:

Bacon ipsum dolor sit amet drumstick turducken sirloin bacon, jowl tail sausage ham hock flank shank pork kielbasa beef hamburger leberkas. Tongue beef ribs ham hamburger fatback chuck pork belly shoulder biltong sausage tenderloin swine porchetta cow. Kielbasa kevin leberkas short loin andouille pork. Pork belly pork short ribs bacon jerky venison, spare ribs brisket sausage flank. Kielbasa salami ham hock leberkas doner. Sausage beef tenderloin venison doner chuck. Sausage beef tongue, chuck pig turkey pork landjaeger porchetta pork belly biltong tri-tip.

then:

echo my_trim_excerpt( $longbacontext, 10, ' [enough bacon]' );

will shorten it to:

Bacon ipsum dolor sit amet drumstick turducken sirloin bacon, jowl [enough bacon]

  • My excerpt fields were indeed empty, how embarrassing! I forgot WordPress will make them on its own when using native functions. The trim function worked out great. I'm still pretty new to all the functions WordPress offers when it comes to displaying things, and often find them all a little confusing when trying to decide which to use. I'll look more closely at WP_Query, but in the mean time will the way I have things setup pose any critical issues? Aside from maybe not functioning as I'm used to! – Preston Feb 14 '14 at 2:22
0

You really should use native WordPress functions (WP_Query, have_posts, the_post the_excerpt etc.)!

However, if you want to stick with your current approach, you might want to do this:

echo apply_filters('the_excerpt', $theid->post_excerpt);
0

I did it another way. I passed the post ID to get the post, and then grab the post_excerpt from it. Finally checked whether there's any set excerpt? If so, show the excerpt, but if not set, show the post content, but control the amount of words using wp_trim_words().

<?php
//Post ID is stored in $post_id, was grabbed from a custom $wpdb query
$queried_post = get_post( $post_id ); //get the specific post only
$queried_excerpt = $queried_post->post_excerpt; // the excerpt
$queried_content = $queried_post->post_content; // the content
echo empty( $queried_excerpt ) ? wp_trim_words($queried_content, 20) : $queried_excerpt;
?>
-1

use

<?php echo $theid->post_excerpt; ?>

instead of

<?php echo $theid->the_excerpt; ?>

For that you have to enter excerpt text from Excerpt metabox in post-editor. If you do so then only excerpt value is stored in post_excerpt field of wp_posts table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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