Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1473566

    Hi there

    I’ve opened a new thread based on my last https://kriesi.at/support/topic/secure-custom-field-after-excerpt/#post-1473534 as I’ve just remembered I need the function to also add the custom fields to the single posts. Is it a quick fix?

    Thanks
    Richard

    #1473727

    Hey Richard,

    Thank you for the inquiry.

    Are you using the Advance Layout Builder for the posts? If not, you can the “the_content” filter to append additional content to the post.

    add_filter('the_content', function ($content) {
        if (is_singular('post')) { 
            $post_id = get_the_ID();
            $event_date = get_post_meta($post_id, 'event_date', true);
            $event_time = get_post_meta($post_id, 'event_time', true);
            $event_location = get_post_meta($post_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>';
    
            $content .= $custom_output;
        }
    
        return $content;
    }, 10);
    

    Best regards,
    Ismael

    #1473746

    Hey Ismael

    Excellent thanks and I’ve combined the two functions for simplicity and posting here if anyone else wants a function that adds custom fields to grid posts (ALB) and single posts (Non-ALB).

    // Helper function to generate the custom event details HTML
    function generate_custom_event_details($post_id) {
        $event_date = get_post_meta($post_id, 'event_date', true);
        $event_time = get_post_meta($post_id, 'event_time', true);
        $event_location = get_post_meta($post_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 $custom_output;
    }
    
    // Filter for avf_post_slider_entry_excerpt
    add_filter('avf_post_slider_entry_excerpt', function ($excerpt, $prepare_excerpt, $permalink, $entry, $context) {
        $custom_output = generate_custom_event_details($entry->ID);
    
        // 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);
    
    // Filter for the_content
    add_filter('the_content', function ($content) {
        if (is_singular('post')) {
            $custom_output = generate_custom_event_details(get_the_ID());
            $content .= $custom_output;
        }
    
        return $content;
    }, 10);
    #1473803

    Hi,

    No worries! Glad we could be of help — thank you for sharing the final code. Let us know if you have more questions.

    Have a nice day.

    Best regards,
    Ismael

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