: This function returns the string. This is used when you need to manipulate the title in PHP (e.g., character limits or conditional logic) before displaying it. Example: echo ' ' . get_the_title() . ' '; 2. Handling Titles Outside the Loop
// Displays the title of the post with ID 3 echo get_the_title(3); Use code with caution. Copied to clipboard 3. Security and Best Practices
Use code with caution. Copied to clipboard php-echo-the-title-3
When echoing titles, security is paramount. While WordPress's core function handles basic sanitization, developers often wrap the output in escaping functions to prevent Cross-Site Scripting (XSS) attacks, especially when the title is used inside an attribute. echo esc_attr( get_the_title() ); Escaping for HTML: echo esc_html( get_the_title() ); Implementation Example
$before : Text or HTML to place before the title (e.g., ). : This function returns the string
The function the_title() serves as a wrapper that automatically "echoes" (prints) the title to the browser. the_title( $before, $after, $echo ); Parameters:
: This function includes an internal echo . You do not need to type echo the_title(); because it handles the output for you. get_the_title()
One of the most common points of confusion for developers at "Level 3" of their learning is the difference between the_title() and get_the_title() .