Custom Excerpt Function

This file contains a WordPress function 'make_better_excerpt' that generates improved excerpts by stripping HTML tags, shortening to 100 characters, and neatly ending with an ellipsis.

<?php /** * Function to create better excerpts * * @param string $content The original content * @return string The modified content */ function make_better_excerpt($content) { $excerpt = strip_tags($content); // Remove HTML tags $excerpt = substr($excerpt, 0, 100); // Shorten to 100 characters $excerpt = substr($excerpt, 0, strripos($excerpt, ' ')); // Cut at the last space $excerpt = rtrim($excerpt); // Remove trailing spaces $excerpt = $excerpt.'...'; // Add ellipsis return $excerpt; } $content = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla quis lorem ut libero malesuada feugiat. Lorem ipsum dolor sit amet, consectetur adipiscing elit."; $better_excerpt = make_better_excerpt($content); echo $better_excerpt; ?>

Frequently Asked Questions

It creates a shortened version of a given content string, optimized for excerpt display in WordPress.