Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1476000

    Hi can you recommend a solution for auto generating excerpts that plays nice with ALB. I haven’t found a function of plugin that can get past the builder.

    Thank you

    #1476003

    Hey finchkelsey,

    Thank you for the inquiry.

    We are not aware of any plugin that has this option, but you can try the following hook in the functions.php file. It retrieves the content of the first Text Block element on the page and sets it as the excerpt.

    function av_set_excerpt_alb($post_id) {
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
        if (wp_is_post_revision($post_id)) return;
    
        $post = get_post($post_id);
        if (!$post || empty($post->post_content)) return;
    
        if (preg_match('/\[av_textblock.*?\](.*?)\[\/av_textblock\]/s', $post->post_content, $matches)) {
            $text_content = wp_strip_all_tags($matches[1]); 
    
            $excerpt = wp_trim_words($text_content, 55, '...');
    
            remove_action('save_post', 'av_set_excerpt_alb'); 
            wp_update_post([
                'ID'           => $post_id,
                'post_excerpt' => $excerpt,
            ]);
            add_action('save_post', 'av_set_excerpt_alb');
        }
    }
    add_action('save_post', 'av_set_excerpt_alb');
    

    Best regards,
    Ismael

    #1476004

    Hi thank you,

    Should I expect to see the auto generated text in the excerpt box on the page or is this happening on the fly.

    I do not see the excerpts in the search results page after implementing the function.

    See example below.

    Thank you

    #1476005

    Hey!

    You may need to edit the posts and update them. The excerpt will not be visible in the Excerpt field, but it will be saved as post_excerpt in the database.

    Best regards,
    Ismael

    #1476007

    Hey!

    Thanks its close.

    It is forcing shortcodes into some of the on page excerpt field boxes that I can’t overwrite with an edit and save.

    See page link from above.

    I would a hierarchy of on page excerpts at the top, thoughts?

    Thank you

    #1476057

    Hey!

    Thank you for the inquiry.

    As mentioned above, the code will look for the first Text Block in the page builder and save it as the post excerpt. I assumed your very first Text Block on the page will contain your most important content, but if that’s not the case, you will need to edit the page and adjust the position of the elements, making sure that the very first Text Block contains the content you want to retrieve.

    Let us know if you need more info.

    Cheers!
    Ismael

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