ID ) ) { $excerpt = wp_trim_words( $post->post_excerpt, $length, '...' ); } else { // Get content and strip blocks/shortcodes $content = $post->post_content; $content = strip_shortcodes( $content ); $content = preg_replace( '//', '', $content ); // Remove HTML comments (blocks) $content = wp_strip_all_tags( $content ); $excerpt = wp_trim_words( $content, $length, '...' ); } // Re-add filter if it exists if ( function_exists( '_block_template_render_without_post_block_context' ) ) { add_filter( 'render_block_context', '_block_template_render_without_post_block_context', 1 ); } return $excerpt; } catch ( Exception $e ) { // Fallback to simple content extraction $content = wp_strip_all_tags( $post->post_content ); return wp_trim_words( $content, $length, '...' ); } } /** * Load template file and render with data * * @param string $template_type Type of template (blog, filter-bar) * @param string $template_name Template name (template1, template2, custom, etc.) * @param array $data Data to pass to template * @return string Rendered HTML */ function asfp_load_template( $template_type, $template_name, $data = [] ) { // Extract data to variables extract( $data ); // Build template path $template_file = dirname( dirname( __FILE__ ) ) . '/templates/' . $template_type . '/' . $template_name . '.php'; // Check if template file exists if ( ! file_exists( $template_file ) ) { return ''; } // Start output buffering ob_start(); // Include template file include $template_file; // Get buffered content return ob_get_clean(); } /** * Render a single post item using template files * * @param int $post_id Post ID * @param string $template_result_item Custom HTML template from database * @param string $blog_template Template name (template1, template2, etc.) * @param int $excerpt_length Number of words for excerpt (default: 25) * @param array $display_settings Display settings (show_title, show_excerpt, show_date, show_author, show_image) * @return string Rendered HTML */ function asfp_render_post_item( $post_id, $template_result_item, $blog_template, $excerpt_length = 25, $display_settings = [] ) { $post = get_post( $post_id ); // Default display settings if not provided $show_title = isset( $display_settings['show_title'] ) ? $display_settings['show_title'] : true; $show_excerpt = isset( $display_settings['show_excerpt'] ) ? $display_settings['show_excerpt'] : true; $show_date = isset( $display_settings['show_date'] ) ? $display_settings['show_date'] : true; $show_author = isset( $display_settings['show_author'] ) ? $display_settings['show_author'] : false; $show_image = isset( $display_settings['show_image'] ) ? $display_settings['show_image'] : true; // Prepare template data $thumb_url = get_the_post_thumbnail_url( $post_id, 'full' ); $title = get_the_title( $post_id ); $link = get_permalink( $post_id ); $date = get_the_date( 'M j, Y', $post_id ); // Short month format (e.g., Oct 12, 2025) $author = get_the_author_meta( 'display_name', get_post_field( 'post_author', $post_id ) ); // Ensure excerpt length is valid if ( $excerpt_length <= 0 ) { $excerpt_length = 25; } // Get excerpt - use safe excerpt extraction $excerpt = asfp_get_safe_excerpt( $post, $excerpt_length ); // Get category - use direct database query for lightweight endpoint global $wpdb; $categories = $wpdb->get_results( $wpdb->prepare( "SELECT t.name, t.slug, t.term_id FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id INNER JOIN {$wpdb->term_relationships} AS tr ON tt.term_taxonomy_id = tr.term_taxonomy_id WHERE tr.object_id = %d AND tt.taxonomy = 'category' ORDER BY t.name ASC LIMIT 1", $post_id ) ); $category_name = ! empty( $categories ) ? $categories[0]->name : ''; // Get all categories as comma-separated string $category_names = []; if ( ! empty( $categories ) ) { foreach ( $categories as $cat ) { $category_names[] = $cat->name; } } $post_categories = implode( ', ', $category_names ); // Get all tags as comma-separated string $tags = get_the_tags( $post_id ); $tag_names = []; if ( ! empty( $tags ) ) { foreach ( $tags as $tag ) { $tag_names[] = $tag->name; } } $post_tags = implode( ', ', $tag_names ); // Get post slug and content $post_slug = $post ? $post->post_name : ''; $post_content = $post ? apply_filters( 'the_content', $post->post_content ) : ''; // Get author avatar $author_avatar = ''; if ( function_exists( 'get_avatar_url' ) ) { $author_avatar = get_avatar_url( get_post_field( 'post_author', $post_id ), [ 'size' => 96 ] ); } // Template data array $data = [ 'post_id' => $post_id, 'title' => $title, 'excerpt' => $excerpt, 'link' => $link, 'date' => $date, 'author' => $author, 'author_avatar' => $author_avatar, 'thumb_url' => $thumb_url, 'category_name' => $category_name, 'post_slug' => $post_slug, 'post_content' => $post_content, 'post_categories' => $post_categories, 'post_tags' => $post_tags, 'show_title' => $show_title, 'show_excerpt' => $show_excerpt, 'show_date' => $show_date, 'show_author' => $show_author, 'show_image' => $show_image, ]; // Use custom template if provided if ( $template_result_item ) { $data['custom_html'] = $template_result_item; return asfp_load_template( 'blog', 'custom', $data ); } // Use default template (template1, template2, etc.) if ( ! $blog_template ) { $blog_template = 'template1'; } return asfp_load_template( 'blog', $blog_template, $data ); }