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 the_title(); ?><?php echo mb_strimwidth( get_the_title( ), 0, 60, '...' ); ?>Custom Logo
<?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 the_content(); ?><?php echo get_the_content('Read more'); ?>
<?php echo wp_trim_words ( get_the_content( ), 100, '...' ); ?>Featured Image URL and Alt Text
<?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 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 echo get_template_directory_uri(); ?>Post Link
<?php echo the_permalink( ); ?>Posts Page URL
The URL of the page set as the Posts page under Settings > Reading.
<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>Check If a Number Is Even
$number = 20;
if ($number % 2 == 0) {
print "It's even";
}Blog Page Link
The same code as the Posts page URL above.
<?php echo get_permalink( get_option( 'page_for_posts' ) ); ?>The Loop in a Single Post Template
<?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 echo get_the_author_meta( "display_name" ); ?>Author Description
<?php echo get_the_author_meta( "description" ); ?>The Author’s Avatar
<?php
echo get_avatar( get_the_author_meta( "ID" ) );
?>First Category of a Post
<?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 comments_number( '0', '1 ', ' % ' ); ?>Tag List
For full control over the markup, loop through the tags with get_the_tags() instead.
<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
$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
$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
//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 if( current_theme_supports( "custom-logo" ) ): ?>
<div class="header-logo text-center">
<?php the_custom_logo( ); ?>
</div>
<?php endif; ?>Registration and Login URLs
<?php echo wp_registration_url(); ?>
<?php echo wp_login_url(); ?>Check If a User Is Logged In
<?php if( is_user_logged_in() ): ?>
//do something
<?php endif; ?>Featured Image With a Fallback
When the post has no featured image, the image falls back to product-image-dummy.jpg in the theme’s assets/img folder.
<?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(); ?>">

