Skip to main content

Course Progress

Loading...

📝 Template Tags & Functions - Part 1

Core WordPress template tags for displaying post data

Master the fundamental template tags every theme developer needs

Part 1 Learning Objectives

  • Master the difference between "the_" and "get_" functions
  • Learn core template tags for titles and content
  • Understand link and URL functions
  • Work with date and time template tags

Understanding Template Tags

Template tags are PHP functions provided by WordPress to display information dynamically. They're the bridge between your database content and what users see on your website.

💡
Key Concept
Template tags are your toolkit for displaying content. Think of them as pre-built functions that know how to fetch and format different pieces of post data - from titles and content to metadata and taxonomies.

🔄 The "the_" vs "get_" Pattern

WordPress follows a consistent naming pattern for template tags:

the_* Functions

  • Echo output directly
  • Used in templates
  • Apply filters automatically
  • No return value
<?php the_title(); ?>
// Outputs: My Post Title

get_* Functions

  • Return values
  • Used in PHP logic
  • More flexible
  • Can be stored in variables
<?php 
$title = get_the_title();
echo strtoupper($title);
// Outputs: MY POST TITLE

📄 Title and Content Tags

the_title() / get_the_title()

the_title( $before, $after, $echo )

Displays or retrieves the post title.

Parameter Type Description
$before string Text before title
$after string Text after title
$echo bool Echo or return
<?php the_title('<h1>', '</h1>'); ?>
<h1>My Awesome Post</h1>

the_content() / get_the_content()

the_content( $more_link_text, $strip_teaser )

Displays the post content with formatting.

<?php the_content('Read more...'); ?>

Best Practice

Always use the_content() instead of get_the_content() when displaying content, as it applies essential filters like wpautop and shortcode processing.

the_excerpt() / get_the_excerpt()

the_excerpt()

Displays a short excerpt of the post (55 words by default).

<?php if ( has_excerpt() ) { the_excerpt(); } else { echo wp_trim_words( get_the_content(), 20 ); } ?>

the_title_attribute()

the_title_attribute( $args )

Sanitized version of title for use in attributes.

<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> Link Text </a>

🔗 Link and URL Tags

the_permalink() / get_permalink()

the_permalink( $post )

Displays the permanent link to the post.

<a href="<?php the_permalink(); ?>"> <?php the_title(); ?> </a>

get_post_permalink()

get_post_permalink( $id, $leavename, $sample )

Gets permalink for any post type.

<?php $url = get_post_permalink( 123 ); echo esc_url( $url ); ?>

the_shortlink()

the_shortlink( $text, $title, $before, $after )

Displays a shortlink to the post.

<?php the_shortlink('Short URL', 'Share this post'); ?>

wp_get_shortlink()

wp_get_shortlink( $id, $context, $allow_slugs )

Retrieves the shortlink for a post.

<?php $shortlink = wp_get_shortlink(); echo "Share: " . esc_url($shortlink); ?>

📅 Date and Time Tags

the_date() / get_the_date()

the_date( $format, $before, $after, $echo )

Displays the post date (only once per day in a list).

<?php the_date('F j, Y'); ?> // Output: December 25, 2024
⚠️
the_date() only displays once per day when listing multiple posts. Use get_the_date() for consistent display.

the_time() / get_the_time()

the_time( $format )

Displays the time the post was published.

<?php the_time('g:i a'); ?> // Output: 3:45 pm

the_modified_date()

the_modified_date( $format, $before, $after, $echo )

Displays the date the post was last modified.

<?php if ( get_the_modified_date() != get_the_date() ) { echo 'Updated: '; the_modified_date('F j, Y'); } ?>

human_time_diff()

human_time_diff( $from, $to )

Human-readable time difference.

<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'; // Output: 2 hours ago ?>
➡️
Continue Learning

Ready for more? Continue to Part 2 where we'll cover:

  • Author template tags
  • Category and taxonomy functions
  • Media and attachment tags
  • Custom fields and metadata
Continue to Part 2 →

Part 1 Resources