Get post/page data outside the loop
You’re using non-existent variables in your function, $post_id, $post, $page. Use get_queried_object to get data from the current page. if( is_single() || is_page() ){ $this_page = get_queried_object(); $excerpt = get_post_field( ‘post_excerpt’, $this_page->ID ); }