Home › Forums › Add-ons › Flexible Content Field › Possible to get excerpt from sub_field?
My posts are built with flexible content and I need to show excerpts on archive pages. I’ve shown custom excerpts from fields before, but is it possible to get excerpts from sub_fields? The code I’ve used to get from a field:
function custom_field_excerpt() {
global $post;
$text = get_field(‘text’);
if ( ” != $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters(‘the_content’, $text);
$text = str_replace(‘]]>’, ‘]]>’, $text);
$excerpt_length = 35; // 35 words
$excerpt_more = apply_filters(‘excerpt_more’, ‘ ‘ . ‘…’);
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters(‘the_excerpt’, $text);
}
It should be as simple as this.
function custom_sub_field_excerpt($field) {
$text = get_sub_field($field);
// the rest of your code
}
You also don’t need to use global $post;
in your case since your not using it for anything and ACF automatically does this if you don’t supply a post ID when getting the value of a field.
Hey there. I am having the exact same problem. It is not possible to create a custom excerpt from a sub_field. The solution you suggested won’t work.
This is my code:
function custom_product_excerpt() {
$text = get_sub_field(‘text_left’);
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);
}
The sub field is a WYSIG-Field. I also tried it with a sub field of type textarea. Both don’t work.
Are there any suggestions?
Not sure what I was thinking when I commented the last time. When you your using a sub field you always need to create a loop for the parent field.
if (have_rows('parent_field')) {
while(have_rows('parent_field')) {
the_row();
$text = get_sub_field('sub_field_name');
}
}
The details of what row and sub field you’ll need to work out for yourself, but you cannot use get_sub_field()
without creating a loop.
The topic ‘Possible to get excerpt from sub_field?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.