WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] [closed] How to pull excerpt from Advanced Custom Field (32 posts)

  1. McAsh
    Member
    Posted 2 years ago #

    Hi guys

    I am working with the free 'Boldy' theme from Site5 (http://wordpress.site5.net/boldy/) which has a predefined excerpt from the_content.

    However, I have build a post template that uses the plugin 'Advanced Custom Fields' (http://www.advancedcustomfields.com/) and on this template the editor fill in content in the different fields and thereby not in the_content. This means that there's no excerpt shown for these posts and therefore I want to pull an excerpt from one of the custom fields. I can display an entire Advanced Custom Field by using <?php echo the_field('field_name'); ?> but how can I limit the output of this command to only 20-30 words?

    Any help would be greatly appreciated :) Thanks..

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with this in your theme's functions.php [untested]:

    function custom_field_excerpt() {
    	global $post;
    	$text = the_field('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);
    }

    Change $text = the_field('field_name'); to the correct field name.
    And use this inside the loop on your template files:

    <?php echo custom_field_excerpt(); ?>
  3. McAsh
    Member
    Posted 2 years ago #

    @keesiemeijer:

    Thanks for helping out. However, it doesn't seem to do the trick. I've pasted the function into functions.php, changed 'field_name' to the right name and inserted <?php echo custom_field_excerpt(); ?> into my template.

    This results in the entire field being displayed. The word limit is not taking effect.
    I'm pretty new to PHP so I really appreciate your help :)

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    Try changing:

    $text = the_field('field_name');

    to

    $text = get_field('field_name');
  5. McAsh
    Member
    Posted 2 years ago #

    Works! Thank you :)

    Just one more thing: The excerpt is not displaying three dots when the word limit is reached. How can I display these dots. I you've tried to incorporate them in your code snippet above so I guess it's due to the original excerpt function from my theme which goes as follows:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Can you see what I need to change here in order to make the dots appear?

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    I guess it's due to the original excerpt function from my theme which goes as follows:

    Good guess.

    If you only want three dots change this:

    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');

    to this:

    $excerpt_more = ' ...';
  7. McAsh
    Member
    Posted 2 years ago #

    Works perfectly. Thanks a lot for your help :)

  8. keesiemeijer
    moderator
    Posted 2 years ago #

    No problem. Glad you got it resolved.

  9. mamouneyya
    Member
    Posted 2 years ago #

    @keesiemeijer

    What is this line exactly supposed to do?
    $text = str_replace(']]>', ']]>', $text);
    As I understand, it replaces every ]]> by ]]>, which are the same. If so, then it will do nothing, right?

    Also, it seems to me that we could either use wp_trim_words(), or apply_filters('the_excerpt', $text), but why using the both?

  10. bobeedesign
    Member
    Posted 2 years ago #

    I've tried a few different ways but I can't figure out how I would do this same thing with a repeater field. Any help would be much apprecaited!

  11. Rics1983
    Member
    Posted 2 years ago #

    Thanks to keesiemeijer for this snippet! really useful!
    I will share a little modify that i've applied for my needs.

    If you apply this code to retrieve an excerpt from a custom field that's shared with more pages or posts, you've to define an extra variable:

    $post_or_page_id = '10' //Check your desired page ID number
    $text = get_field('field_name',$post_or_page_id);

    For shared custom field i mean the case when you create a custom field, eg: 'page_text' with WYSIWYG field type, and then you apply the location rules to "Post type"->"is equal to"->"page(or post or another defined custom post type)"

  12. Rics1983
    Member
    Posted 2 years ago #

    @bobeedesign
    Have you tried to merge the keesiemeijer code with the documentation of the plugin?
    http://www.advancedcustomfields.com/docs/functions/the_repeater_field/
    Check out also the other example codes from ACF documentation.

    Remember also that with Repeaters you're working with arrays, so probably you've to catch the subfield variable in this way $text[0] (or $text[1] or $$text[n] depending on the depth of the desired sub field).
    Consider this not affidable as i'm not a long time php rider, so i could be wrong.
    Take this only as an input from me! ;)

  13. keesiemeijer
    moderator
    Posted 2 years ago #

    @mamouneyya

    What is this line exactly supposed to do?

    The code snipped is just an altered wp_trim_excerpt function (used by WordPress for automatic excerpts): http://core.trac.wordpress.org/browser/tags/3.3.2/wp-includes/formatting.php#L1995
    I'm also not really sure what that line of code does.

    [edit] Oh, now I see that line should be:

    $text = str_replace(']]>', ']]>&gt;', $text);

    These forums automatically format special Characters (as it should).

  14. JonMadof
    Member
    Posted 2 years ago #

    @keesiemeijer -

    I've been looking for a way to do this. Thanks so much for posting this solution!

    FYI I'm using the 'more fields' plugin for custom fields, so i replaced this line:
    $text = get_field('field_name');

    with this:
    get_post_meta($post->ID, 'field_name', true);

  15. JeppeBlomgren
    Member
    Posted 2 years ago #

    Hi all. Great post!
    I'm trying to get the excerpt made above to show tags. In other words, i want to show <a>, <strong> etc. in the excerpt. Any ideas on how to add that?

    I tried this. Doesn't work.

    function custom_excerpt() {
    	global $post;
    	$text = get_field('innehall');
    	if ( '' != $text ) {
    		$text = strip_shortcodes( $text );
    		$text = apply_filters('the_content', $text);
    		$allowed_tags = '<p>,<a>,<em>,<strong>,<img>';
    		$text = strip_tags($text, $allowed_tags);
    		$text = str_replace(']]>', ']]>', $text);
    		$excerpt_length = 40; // 20 words
    		$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    		$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    	}
    	return apply_filters('the_excerpt', $text);
    }
  16. keesiemeijer
    moderator
    Posted 2 years ago #

    Allowing html tags can break your site because the_excerpt can cut the content in the middle of a html tag. But if you must, try it with:

    $allowed_tags = '<p><a><em><strong><img>';

    http://php.net/manual/en/function.strip-tags.php

  17. JeppeBlomgren
    Member
    Posted 2 years ago #

    Thanks keeslemeljer. Allthough that didn't seem to do the work. But you have a good point with the possibility of breaking the html.

    Do you, or anyone else have a good suggestion on how to make a advanced cusomt field equivalent to the_content but with word limit and a read more tag without using the devider in the editor? Do you know what I mean?

    In thought, that would make an excerpt but with the "the_content" tag instead. That wouldn't break my html would it?

    To pull data from my advanced custom field wysivyg editor I use <?php the_field('innehall'); ?>. How can I put an automatic word limit to that tag and also display a reda more tag if it exceeds the word limit?

    Thanks again!
    Jeppe

    http://jeppeblomgren.se

  18. keesiemeijer
    moderator
    Posted 2 years ago #

    Why not use a manual excerpt if it's equivalent to the_content but with word limit? Manual excerpts can have html tags.

    http://codex.wordpress.org/Excerpt#Excerpt.2C_automatic_excerpt.2C_and_teaser

  19. JeppeBlomgren
    Member
    Posted 2 years ago #

    Aha, so manual excerpt shows tags? Didn't know that. I'll try it.

    Thanks again
    /Jeppe

  20. JeppeBlomgren
    Member
    Posted 2 years ago #

    Hi again. That still doesnt do it for me the way i need this. You see, my client can't or isn't supposed to know how to write tags. If I make manual excerpts she will have to write the tags herself. That is going to end up in my lap anyway. Så what I need is an automatic excerpt (she writes really long posts) for the first page. But at the same time I want especially links to work in the excerpt since some post are very short, and doesnt excced the word limit, hence does not not get a read more link. If the link isn't visible and you cant klick to go se the post where link is clickable - its useless.

    I guess I'm trying to make the site idiot proof. Any more suggestions?

    Cheers / Jeppe

  21. keesiemeijer
    moderator
    Posted 2 years ago #

  22. Iamhere
    Member
    Posted 2 years ago #

    Hi all - great post!
    Hope you won't mind me jumping in with slightly different question, but same plugin and topic (excerpts)

    I am using ACF and need to be able to display search results excerpts - I've tried with Search Everything plugin - which does provide search results (at least something) for the custom field. So now I have search result but it only displays the post title - no excerpt text at all http://wordpress.org/extend/plugins/search-everything/

    I need to display an excerpt as per normal search. But I think this issue is to do with ACF not Search Everything.

    I am unable to manually create excerpts for the custom fields because the user (member) have control over the post contents and update it regualarly.

    Anybody got a suggestions please - I need to fix this so I can sleep!
    Thank you in advance.

  23. @Iamhere, rather than trying to change the topic on a resolved thread, please start your own instead.

    You'll get better support that way.

    http://wordpress.org/support/plugin/search-everything#postform

  24. Iamhere
    Member
    Posted 2 years ago #

    @jan

    Thank you for your opinion, though I feel this is not a different topic.
    "pull excerpt from Advanced Custom Field" is exactly what I need in the search results.

    Also, the very audience I am trying to communicate with (experience with ACF and excerpts) are in this topic and will probably find it useful to know the answer, since this issue is common to all ACF users.

    Nonetheless, I will admit I hadn't noticed it was marked resolved (and in which case, why is it not closed?)

  25. Thank you for your opinion,

    You're welcome. ;)

    though I feel this is not a different topic.

    It is a different topic.

    You do want us other volunteers to focus on your problem, right? Starting your own thread in that plugin's forum will get you better support for that plugin. This thread isn't about that plugin.

    (and in which case, why is it not closed?)

    Resolved topics aren't automatically closed as far as I know. That way the OP can come back and say "Whoops, my mistake! Post #12345 didn't really solve it for me. Any other ideas now that I've tried that?"

    You get the idea.

  26. venkmanuk
    Member
    Posted 1 year ago #

    @keesiemeijer thanks for this - it would be great if there was a way to define the field_name in the actual template file, so if there is more than one field that needs shortened we don't need loads of instances of the snippet in functions.php.

    an alternate to $text = get_field('field_name');
    so I could use something like <?php echo custom_field_excerpt('field_name'); ?>

    is that possible?! :)

  27. venkmanuk
    Member
    Posted 1 year ago #

    function custom_field_excerpt($title) {
    			global $post;
    			$text = get_field($title);
    			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 use <?php echo custom_field_excerpt('field_name'); ?>

  28. mattrock1
    Member
    Posted 1 year ago #

    Beauty post. Thanks keesiemeijer!

  29. mydanceguide
    Member
    Posted 1 year ago #

    Thanks keesiemeijer for the code snippet on displaying custom field excerpts. You rock. perfect solution to my little problem.

  30. bondjack
    Member
    Posted 1 year ago #

    The expert code Venkmanuk posted works as a charm for most of my pages. But in the occasions I want to us on a page where it is depending on its post id id doesnt work. For the ACF code it is easy to fix: <?php the_field('acfname', $value->ID); ?>
    So I add the =>ID of the page and it load the value that is in the afc field.
    For the custom_field_expert it doesn't work:
    <?php echo custom_field_excerpt('afcname', $value->ID); ?>
    It still shows no text.

    any ideas?

Topic Closed

This topic has been closed to new replies.

About this Topic