post Excerpt

Support General post ExcerptResolved

Viewing 15 posts - 1 through 15 (of 23 total)
  • Author
    Posts
  • #18709
    BouzinBouzin
    Participant

    Hi,

    I'm looking to have post Excerpt with textarea

    if possible automatically without having to retype the text

    Thank you in advance for your support
    Best regards

    #18712
    Long NguyenLong Nguyen
    Moderator

    Hi,

    The WordPress core has the function the_excerpt or get_the_excerpt to show the excerpt of the post as well. I just think no need to create a field to get the post excerpt.

    #18731
    hartsook@gmail.com[email protected]
    Participant

    I think what is wanted here is something I was going to ask as well.

    I have several textarea MB fields (actually in a custom post type, but could also apply to a standard post).

    What I want is for the MB textarea field to automatically be linked to the standard WP Excerpt field in much the same way that you linked a custom image field to the User Profile image.

    The reason for this in my case is that the custom post type only has the custom fields displayed through a Beaver Themer template, I don't use the standard post content area at all. As a result on post archive pages when the loop is trying to get the excerpt (or the first n words of the post content) it doesn't find anything. To avoid having to copy the text from one of the textarea custom fields and paste it into the post excerpt field it would be great to have the excerpt field automatically get populated with that custom field.

    Can this be done similar to the way you did the User Profile image?

    #18732
    BouzinBouzin
    Participant

    Thank you for intervening that's exactly it. I use Brizy builder but it's the same request.

    #18733
    Anh TranAnh Tran
    Keymaster

    Hi, we have a tutorial for doing this here. It's for post content, but it also works for post excerpt if you change the field ID in the tutorial to excerpt.

    Here is the code: https://pastebin.com/t3tBWcG6

    #18737
    hartsook@gmail.com[email protected]
    Participant

    hmmm, almost...
    I'm actually using a wysiwyg field instead of a textarea field, so I tried to adapt your sample code to incorporate the wysiwyg field replacing the Post Content example. But I'm missing something, it's not working

    So to simplify, I tried to conform to your example by adding a textarea field to the CPT called "Snippet Excerpt", snippet-excerpt

    • The CPT singular is called Code Snippet code-snippet
    • the field group is "Code Snippet before" code-snippet-before
    • The wysiwyg field is called "Reason for snippet" reason-for-snippet
    • The textarea field is called "Snippet Excerpt" snippet-excerpt

    The code below does indeed remove the excerpt field from the Code Snippet CPT, but does leave it for standard posts which is what I want.

    But text entered in the textarea custom field does not display as the post_excerpt in the loop even though text that was entered in the standard Excerpt field in older posts still shows even though the excerpt metabox is no longer displayed when editing those posts.

    It's probably something simple, but I can't find it. Here's the code that's not working:

    /**
    * using custom field for Code Snippet CPT excerpt
    **/
    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
        $meta_boxes[] = [
            'title'      => 'Code Snippet Before',
            'post_types' => 'code-snippet',
            'fields'     => [
                [
                    'type' => 'textarea',
                    'id'   => 'excerpt', // This is the must!
                    'name' => 'Snippet Excerpt',
                ],
            ],
        ];
    
    return $meta_boxes;
    } );
    
    add_filter( 'rwmb_excerpt_field_meta', function() {
        $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
        return get_post_field( 'post_excerpt', $post_id );
    } );
    add_filter( 'rwmb_excerpt_value', '__return_empty_string' );

    What am I missing?

    It would be terrific if you could also show how to modify the code so that we could use a wysiwyg field instead of a textarea field -- since we would want html-formatted text displaying as the excerpt. In my particular case the wysiwyg custom field would be "Reason for snippet" reason-for-snippet in the Code Snippet Before field group.

    #18738
    hartsook@gmail.com[email protected]
    Participant

    Here's how I tried to adapt your wysiwyg field to post content example to fit my wysiwyg field to post excerpt. Not working, it removes excerpt field from CPT, but does not return the wysiwyg field content to the post_excerpt in a loop.

    
    // Remove the editor. Change 'post' to your custom post type.
    add_action( 'init', function () {
        remove_post_type_support( 'code-snippet', 'excerpt' , 'post_content' ); // also tried to remove the post_content editor for the CPT, but it still shows
    } );
    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
    // was not sure if the following 3 lines were necessary so commented them out, did not work either way
        // Get the current post content and set as the default value for the wysiwyg field.
        // $post_id = filter_input( INPUT_GET, 'code-snippet', FILTER_SANITIZE_NUMBER_INT );
       //  $post_content = get_post_field( 'post_excerpt', $post_id );
        $meta_boxes[] = [
            'title'      => 'Code Snippet Before', // this is the Field Group Title
            'post_types' => 'code-snippet', // Change 'post' to your custom post type.- done
            'fields'     => [
                // Register a wysiwyg field of which the content is saved as post content.
                [
                    'type' => 'wysiwyg',
                    'id'   => 'excerpt', // This is the must!
                    'name' => 'Reason for snippet', // label for the custom field
                    'std'  => $post_excerpt, // was not sure what to put here
                ],
                // Custom style to overwrite the editor style set by WordPress.
                [
                    'type' => 'custom_html',
                    'std'  => '<style>#wp-content-editor-tools{background:none;padding-top:0;}</style>',
                ],
            ],
        ];
        return $meta_boxes;
    } );
    // Set the value for the 'excerpt' field.
    add_filter( 'rwmb_content_field_meta', function() {
        $post_id = filter_input( INPUT_GET, 'code-snippet', FILTER_SANITIZE_NUMBER_INT ); //assumed I should use code-snippet the CPT instead of 'post' here
        return get_post_field( 'post_excerpt', $post_id ); // assumed I should use 'post_excerpt instead of 'post_content here
    } );
    // did not think the following were necessary in this case so commented them out
    // Do not save 'content' field to post meta.
    // add_filter( 'rwmb_content_value', '__return_empty_string' );
    
    #18740
    hartsook@gmail.com[email protected]
    Participant

    here's the code in pastebin, maybe it's easier to read?
    https://pastebin.com/7tmTTPU1

    #18741
    Anh TranAnh Tran
    Keymaster

    I made a short video to explain things here:

    https://www.loom.com/share/0b651f654cce47aa82419cd91631e100

    And here is the code for both content and excerpt:

    https://pastebin.com/1tL0bNhW

    #18760
    BouzinBouzin
    Participant

    Hi,

    I am not very technical and I do not code. I use your builder.

    I tried but something is not working.

    If i put id=content the field appears odd and no longer works.

    Would you have a tutorial for the builder version without code. You would be awesome 🙂

    Screenshot_2020-03-30_Edit_Field_Group_‹DEMO—_WordPress(3).png

    thank you again for your help
    greetings

    #18764
    hartsook@gmail.com[email protected]
    Participant

    getting very close 🙂

    Since I prefer to use the MB Builder to create the CPT and Custom Fields, I removed that part of the php code you put in pastebin so I'm left with something that works great except for one problem using Beaver Themer to layout the front end. Here is the code I'm using:

    /**
    * In MB Builder I have a custom field replace the post <strong>content</strong> by setting custom field "Reason for Snippet", wysiwyg field ID to content
    * I removed the custom post excerpt field from code snippets custom post type <strong>done through unchecking box in CPT setting </strong>
    * I removed the custom post content editor field from code snippets CPT <strong>done through unchecking box in CPT setting </strong>
    **/
    // what I'm left with is:
    // This is for content
    add_filter( 'rwmb_content_field_meta', function() {
        $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
        return get_post_field( 'post_content', $post_id );
    } );
    add_filter( 'rwmb_content_value', '__return_empty_string' );

    The custom field "Reason for Snippet" field content now shows up on the front end in Beaver Builder layouts that display posts where the excerpt would be. It turns out this is better than making that field the Excerpt because the manual excerpt field overrides controls on the front end like how many words are displayed. The Post modules in Beaver Builder for example will grab the post content and use it as an excerpt and you can set it to display say the first 25 words of the content if you want. If the custom field becomes the Excerpt then the whole field content is displayed instead regardless of the excerpt length setting. Also RSS feeds will grab part of the content when the Excerpt field is empty. So it's better to use the custom field as the content instead of the excerpt for my usage. But an advantage is a manual Excerpt field can have html which gets stripped out when grabbing the first n words of the content.

    Now for the problem I ran into with Beaver Themer. The wysiwyg field with ID set to content allows text formatting in both the visual and text tabs including Add Media, etc. But trying to display the MB custom field on the front end doesn't work.

    Using the Themer connector in a text or html module if I select a metabox field I see the Reason for Snippet field, and when I insert it I get: [wpbb post:meta_box field='content'] but nothing shows on the front end even if there is content in that custom field. The workaround is to connect with the Post Content [wpbb post:content]. Then everyting shows up ok fully formatted with and the images too. It's confusing not to be able to select the MB field, but not the end of the world if you know to use the Post Content connector instead.

    #18766
    Anh TranAnh Tran
    Keymaster

    @hartsook:

    #1. The thing with excerpt/content and which is chosen to display is probably another thing from WordPress in general, not BB or Meta Box. I think it's better to put it here for clarity:

    If you display post excerpt: in BB, you might want to use text/html module and connect to post excerpt (not Meta Box field), then it will does the following thing:

    • If the post excerpt is not empty -> take it
    • If the post excerpt is empty -> generate excerpt from the post content

    In both case, the excerpt length will be still applied, e.g. the text displayed is truncated to the specific length of your choice.

    It's the default behavior in WordPress in general. And it's confusing for a lot of people!

    If you display post content: then only text in the post content is get and displayed. Nothing is confusing here.

    #2. The other thing is using the field connector in BB

    When we do all the things in the previous replies, Meta Box think these fields are for post content and post excerpt and doesn't store any value in the custom fields. Therefore, you must connect the text/html module in BB to post content/post excerpt instead of Meta Box Fields.

    This is what you already did, and correctly. I just make it clearer!

    #18767
    Anh TranAnh Tran
    Keymaster

    @B: You don't need to set custom attributes, all you need to do in MB Builder is set the field ID to content or excerpt, like this: https://imgur.com/998hlgP. Nothing more.

    And if you can, add this code to your theme's functions.php file. Or you can install the Code Snippet plugin (free on .org) and add the following code:

    add_filter( 'rwmb_content_field_meta', function() {
        $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
        return get_post_field( 'post_content', $post_id );
    } );
    add_filter( 'rwmb_content_value', '__return_empty_string' );
    
    add_filter( 'rwmb_excerpt_field_meta', function() {
        $post_id = filter_input( INPUT_GET, 'post', FILTER_SANITIZE_NUMBER_INT );
        return get_post_field( 'post_excerpt', $post_id );
    } );
    add_filter( 'rwmb_excerpt_value', '__return_empty_string' );
    #18793
    BouzinBouzin
    Participant

    Hi,

    Thank you it works for me 🙂

    Your plugins are awesome

    And thank you very much for your patience

    But I have one last question
    And this normal behavior, I found a behavior that I do not understand

    If the position and placed before or just after the title it does not work

    But if it is placed after the content it works

    #18796
    BouzinBouzin
    Participant

    Meta Box

Viewing 15 posts - 1 through 15 (of 23 total)
  • You must be logged in to reply to this topic.