Frequently Used WordPress Theme Codes

Quick Summary

  • Short WordPress theme snippets cover post titles, content, featured images, dates, links, author details, categories, tags, logos and logged-in checks.
  • Tags that read post data, such as title, content and author, need the current post, so use them inside the Loop.
  • get_the_content() returns the content instead of printing it, so it needs echo, and get_template_directory_uri() points to the parent theme in a child theme.

This is a quick reference of WordPress template tags and short PHP snippets that come up again and again when building a custom theme: post title, content, featured image, date, links, author details, categories, tags, logo and a few conditional checks. Tags that read post data, such as the title, content and author, need the current post, so use them inside the Loop. For WooCommerce templates, see the companion list of frequently used WooCommerce snippets.

Post Title

The second version cuts long titles to 60 characters with an ellipsis.

PHP
<?php the_title(); ?>
PHP
<?php echo mb_strimwidth( get_the_title(  ), 0, 60, '...' ); ?>
PHP
<?php if ( function_exists( 'the_custom_logo' ) ) {
  the_custom_logo();
} ?>

Post Content

The first snippet prints the full content and the last one its first 100 words. get_the_content() in the middle one returns the content instead of printing it, so it needs echo.

PHP
<?php the_content(); ?>
PHP
<?php echo get_the_content('Read more'); ?>

 

PHP
<?php echo wp_trim_words ( get_the_content(  ), 100, '...' ); ?>
PHP
<?php the_post_thumbnail_url(  ); ?>
<?php //ALT TEXT FEATURED IMAGE ?>
<?php echo get_post_meta( get_post_thumbnail_id(), '_wp_attachment_image_alt', true ); ?>

Publish Date

PHP
<?php echo get_the_date(); ?>

Theme Folder URL

In a child theme this still points to the parent theme; stylesheet vs template directory functions shows the functions that point to the child.

PHP
<?php echo get_template_directory_uri(); ?>

Post Link

PHP
<?php echo the_permalink(  ); ?>

Posts Page URL

The URL of the page set as the Posts page under Settings > Reading.

PHP
<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>

Check If a Number Is Even

PHP
$number = 20;
if ($number % 2 == 0) {
  print "It's even";
}

The same code as the Posts page URL above.

PHP
<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>

The Loop in a Single Post Template

PHP
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


<h1><?php the_title(  ); ?></h1>
<p> <?php the_content(); ?></p>


<?php endwhile; endif;  ?>

Author Name

PHP
<?php echo get_the_author_meta( "display_name" ); ?>

Author Description

PHP
<?php echo get_the_author_meta( "description" ); ?>

The Author’s Avatar

PHP
<?php 
echo get_avatar( get_the_author_meta( "ID" ) );
?>

First Category of a Post

PHP
<?php
   $categories = get_the_category();
?>

<a href="
<?php echo esc_url( get_category_link( $categories[0]->term_id ) ); 
?>">

<?php echo esc_html( $categories[0]->name ); ?>

</a>

Comment Count

PHP
<?php comments_number( '0', '1 ', ' % ' ); ?>

Tag List

For full control over the markup, loop through the tags with get_the_tags() instead.

PHP
<span>
<?php echo get_the_tag_list( "<ul class=\"list-unstyled tag-stx-single-view\"><li>", "<li>,</li>", "</li></ul>" ) ?>
</span>

Previous Post Link

This and the next snippet check that a neighboring post exists, so nothing is printed on the first and last post, and the image only appears when that post has a featured image. Previous and next links with a fallback message show a message instead.

PHP
<?php
   $prevPost = get_previous_post();
?>
<?php if ( $prevPost ) : ?>
<?php
$p_t_image = wp_get_attachment_image_src( get_post_thumbnail_id( $prevPost->ID ), 'single-post-thumbnail' );
?>
<a href="<?php echo get_permalink( $prevPost->ID ); ?>" class="single-post-nav">
<?php echo get_the_title($prevPost) ?>
</a>
<?php if ( $p_t_image ) : ?>
<img class="post-nav-img" src="<?php echo $p_t_image[0]; ?>" alt="">
<?php endif; ?>
<?php endif; ?>

Next Post Link

PHP
<?php
$nextPost = get_next_post();
?>
<?php if ( $nextPost ) : ?>
<?php 
$n_t_image = wp_get_attachment_image_src( get_post_thumbnail_id( $nextPost->ID ), 'single-post-thumbnail' );
?>
<a href="<?php echo get_permalink( $nextPost->ID ); ?>" class="single-post-nav">
<?php echo get_the_title($nextPost) ?>
</a>
<?php if ( $n_t_image ) : ?>
<img class="post-nav-img" src="<?php echo $n_t_image[0]; ?>" alt="">
<?php endif; ?>
<?php endif; ?>

First Category of a Custom Post

PHP
<?php
   //change work-category with your taxonomy slug
   $arr = get_the_terms( $id, 'work-category' );
   $first = is_array( $arr ) ? $arr[0] : null;
   ?>
<?php if ( !empty($first) ): ?>
<span class="tag">
<?php esc_html_e( $first->name, $domain = 'creon-nmh47' ); ?>        
</span>
<?php endif ?>

Custom Logo With a Theme Support Check

PHP
<?php if( current_theme_supports( "custom-logo" ) ): ?>
      <div class="header-logo text-center">
           <?php the_custom_logo( ); ?>
      </div>
<?php endif; ?>

Registration and Login URLs

PHP
<?php echo wp_registration_url(); ?>
<?php echo wp_login_url(); ?>

Check If a User Is Logged In

PHP
<?php if( is_user_logged_in() ): ?>
//do something
<?php endif; ?>

When the post has no featured image, the image falls back to product-image-dummy.jpg in the theme’s assets/img folder.

PHP
<?php
$p_dummy_url = get_template_directory_uri();
$p_dummy_url .=  "/assets/img/product-image-dummy.jpg";
?>


<img src="<?php if(has_post_thumbnail()): ?> 
          <?php the_post_thumbnail_url( 'full' ); ?> 
          <?php else: ?>
          <?php echo $p_dummy_url; ?>
          <?php endif; ?>"
alt="<?php the_title(); ?>">
Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More