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

    Hi there

    I’ve been trying to add custom fields so that they show below the excerpt on both the blog grid and single posts using the following code. I’ve added it to the child theme function but I can’t get it to work. Any pointers would be great, thanks.

    add_filter('the_excerpt', function ($excerpt) {
        // Check if we're in a post and want custom fields added
        if (is_single() || is_home() || is_archive()) { 
            // Fetch custom fields
            $event_date = get_post_meta(get_the_ID(), 'event_date', true);
            $event_time = get_post_meta(get_the_ID(), 'event_time', true);
            $event_location = get_post_meta(get_the_ID(), 'event_location', true);
    
            // Create custom field output with individual classes
            $custom_output = '<div class="custom-event-details">';
            if ($event_date) {
                $custom_output .= '<p class="event-date"><strong>Event Date:</strong> ' . esc_html($event_date) . '</p>';
            }
            if ($event_time) {
                $custom_output .= '<p class="event-time"><strong>Event Time:</strong> ' . esc_html($event_time) . '</p>';
            }
            if ($event_location) {
                $custom_output .= '<p class="event-location"><strong>Event Location:</strong> ' . esc_html($event_location) . '</p>';
            }
            $custom_output .= '</div>';
    
            // Append custom fields to the excerpt
            return $excerpt . $custom_output;
        }
    
        // Return the original excerpt for other types of content
        return $excerpt;
    });
    #1473409

    Hey Richard,

    Thank you for the inquiry.

    Where are you testing this? Please provide the page URL in the private field. Are you using the Advance Layout Builder for the posts? If yes, then you may need to define the excerpt manually in the Excerpt field.

    Best regards,
    Ismael

    #1473417

    Hi Ismael

    Thanks for the reply and I’m using the builder. Here’s the URL and it’s the three column grid posts sections.

    Richard

    #1473463

    Hi,

    Thank you for the update.

    We adjusted the filter a bit — please try it again:

    add_filter('avf_post_slider_entry_excerpt', function ($excerpt, $prepare_excerpt, $permalink, $entry, $context) {
        $event_date = get_post_meta($entry->ID, 'event_date', true);
        $event_time = get_post_meta($entry->ID, 'event_time', true);
        $event_location = get_post_meta($entry->ID, 'event_location', true);
    
        $custom_output = '<div class="custom-event-details">';
        if ($event_date) {
            $custom_output .= '<p class="event-date"><strong>Event Date:</strong> ' . esc_html($event_date) . '</p>';
        }
        if ($event_time) {
            $custom_output .= '<p class="event-time"><strong>Event Time:</strong> ' . esc_html($event_time) . '</p>';
        }
        if ($event_location) {
            $custom_output .= '<p class="event-location"><strong>Event Location:</strong> ' . esc_html($event_location) . '</p>';
        }
        $custom_output .= '</div>';
    
        return $excerpt . $custom_output;
    }, 10, 5);
    
    

    If you don’t see any changes, try to manually define the summary of the posts in the Excerpt field.

    Best regards,
    Ismael

    #1473479

    Hey Ismael, that’s great thanks it worked. Although the fields appeared after the Read More button so I have adjusted the code. Not sure if it’s the most tidy way of doing it though.

    add_filter('avf_post_slider_entry_excerpt', function ($excerpt, $prepare_excerpt, $permalink, $entry, $context) {
        $event_date = get_post_meta($entry->ID, 'event_date', true);
        $event_time = get_post_meta($entry->ID, 'event_time', true);
        $event_location = get_post_meta($entry->ID, 'event_location', true);
    
        // Prepare custom output
        $custom_output = '<div class="custom-event-details">';
        if ($event_date) {
            $custom_output .= '<p class="event-date"><strong>Event Date:</strong> ' . esc_html($event_date) . '</p>';
        }
        if ($event_time) {
            $custom_output .= '<p class="event-time"><strong>Event Time:</strong> ' . esc_html($event_time) . '</p>';
        }
        if ($event_location) {
            $custom_output .= '<p class="event-location"><strong>Event Location:</strong> ' . esc_html($event_location) . '</p>';
        }
        $custom_output .= '</div>';
    
        // Check if the "Read More" link exists in the excerpt
        $read_more_position = strpos($excerpt, '<a');
        if ($read_more_position !== false) {
            // Insert the custom output before the "Read More" link
            $before_read_more = substr($excerpt, 0, $read_more_position);
            $read_more_and_after = substr($excerpt, $read_more_position);
            return $before_read_more . $custom_output . $read_more_and_after;
        }
    
        // Append custom output if no "Read More" link is found
        return $excerpt . $custom_output;
    }, 10, 5);
    #1473534

    Hi,

    Glad to know you’ve found a working solution! The modification looks good. Please feel free to open another thread if you have more questions about the theme.

    Have a nice day.

    Best regards,
    Ismael

Viewing 6 posts - 1 through 6 (of 6 total)
  • The topic ‘Secure Custom Field after excerpt’ is closed to new replies.