0

I am trying to display the two most recent posts excerpts in a header widget. Some of the posts are going to be regular blog posts and others are of a custom post type which also utilises the Advanced Custom Fields plugin. The custom post has 4 fields one of which is the wysiwyg text area for the content - of which I need the excerpt to generate from.

I have registered the 'excerpt' as a support and on the custom post type it shows up empty ready for manual input, but I don't want my client to have to manually write an excerpt I want it to generate from the content as the normal posts seem to do just fine with any basic excerpt widget.

I have tried out many widgets that say they offer custom post type excerpt functionality including the ACF Recent Posts Widget which is supposed to do just what I want. I can't seem to find the solution on their support forum either.

The point is I seem to be missing someway to generate an excerpt from a custom post using a custom fields content - is there a way to do this?

I should note I'm relatively new to wordpress and php so sorry if this is obvious or poorly constructed. Happy to provide any of my code if it helps - just not sure what to copy here as it's more of a general question involving custom post types and ACF plugin.

Thanks for any help!

0

SOLVED

Use this code in the function.php file

function custom_field_excerpt() {
global $post;
$text = get_field('description'); //Replace 'your_field_name'
if ( '' != $text ) {
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $excerpt_length = 20; // 20 words
    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('the_excerpt', $text);
}

Then call the function in a custom WP_query using all post types in the header and call wp_reset_postdata(); when you're done instead of using a widget!

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.