WordPress.org

Support

Support » Themes and Templates » [Resolved] How to pull excerpt from Advanced Custom Field

[Resolved] How to pull excerpt from Advanced Custom Field

  • 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..

Viewing 15 replies - 1 through 15 (of 31 total)
  • keesiemeijer
    moderator

    @keesiemeijer

    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(); ?>

    @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 🙂

    keesiemeijer
    moderator

    @keesiemeijer

    Try changing:

    $text = the_field('field_name');

    to

    $text = get_field('field_name');

    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?

    keesiemeijer
    moderator

    @keesiemeijer

    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 = ' ...';

    Works perfectly. Thanks a lot for your help 🙂

    keesiemeijer
    moderator

    @keesiemeijer

    No problem. Glad you got it resolved.

    mamouneyya
    Member

    @mamouneyya

    @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?

    bobeedesign
    Member

    @bobeedesign

    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!

    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)”

    @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! 😉

    keesiemeijer
    moderator

    @keesiemeijer

    @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).

    @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);

    JeppeBlomgren
    Member

    @jeppeblomgren

    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);
    }
    keesiemeijer
    moderator

    @keesiemeijer

    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

Viewing 15 replies - 1 through 15 (of 31 total)
  • The topic ‘[Resolved] How to pull excerpt from Advanced Custom Field’ is closed to new replies.