get_the_title() vs the_title()

En WordPress casi todos los Template Tags tienen dos versiones: the_*() que imprime directamente con echo, y get_the_*() que devuelve el valor para usarlo en PHP. Conocer la diferencia evita muchos errores.

La regla

  • the_title() → hace echo internamente, no devuelve nada útil
  • get_the_title() → devuelve el string, tú decides qué hacer con él

Cuándo usar the_ (imprimir directamente)

// En el HTML, cuando solo quieres mostrarlo
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php the_excerpt(); ?>
<?php the_permalink(); ?>

Cuándo usar get_the_ (necesitas el valor)

// En atributos HTML — necesitas escapar
<img alt="<?php echo esc_attr( get_the_title() ); ?>">

// Para concatenar con otros strings
$meta_title = get_the_title() . ' | ' . get_bloginfo('name');
echo '<title>' . esc_html( $meta_title ) . '</title>';

// Para comparar o procesar en PHP
$titulo = get_the_title();
if ( strlen( $titulo ) > 60 ) {
    $titulo = substr( $titulo, 0, 57 ) . '...';
}

// Para un post específico (pasando el ID)
$titulo_relacionado = get_the_title( 42 );

// En wp_localize_script (pasar a JS)
wp_localize_script( 'mi-script', 'datos', [
    'titulo' => get_the_title(),
]);

El mismo patrón en otros Template Tags

the_permalink()     → get_permalink()
the_content()       → get_the_content()
the_excerpt()       → get_the_excerpt()
the_date()          → get_the_date()
the_author()        → get_the_author()
the_post_thumbnail()→ get_the_post_thumbnail()