Frequently Used WordPress Theme Codes

Share Now

1. Blog/Post Title

<?php the_title(); ?>
<?php echo mb_strimwidth( get_the_title(  ), 0, 60, '...' ); ?>

1.1. Custom Logo

<?php if ( function_exists( 'the_custom_logo' ) ) {
  the_custom_logo();
} ?>

2. Blog/Post Content

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

 

<?php echo wp_trim_words ( get_the_content(  ), 100, '...' ); ?>

3. Blog/Post Featured Image

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

4. Blog/Post Publish Date

<?php echo get_the_date(); ?>

5. Template Directory

<?php echo get_template_directory_uri(); ?>

6. Blog/Post Link

<?php echo the_permalink(  ); ?>

6.1. Blog/Post Page Link/URL

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

7. Check If Number Is Even

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

8. Blog Page Link

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

9. Single Blog Condition

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


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


<?php endwhile; endif;  ?>  

10. Blog/post author name

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

10.1. Blog/post Author Description

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

10.2. Blog/post Author Avatar

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

11. Show 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>

12. Post’s Comments Count

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

13. Bog Post’s Tag List

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

14. Previous Post Link On Single Post Page

<?php
   $prevPost = get_previous_post();
?>
<?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>
<img class="post-nav-img" src="<?php echo $p_t_image[0]; ?>" alt="">

15. Next Post Link On Single Post Page

<?php
$nextPost = get_next_post();
?>
<?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>
<img class="post-nav-img" src="<?php echo $n_t_image[0]; ?>" alt="">

16. First Category Of A Custom Post

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

17. Custom Logo

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

18. Registration and Login Page Link

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

19. Check If User Logged In or Not

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

20. Check If Post Has Thumbnail or Not

<?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(); ?>">
Picture of Nahid Mahamud

Nahid Mahamud

Web Developer | Graphic Designer | WordPress & Woo-commerce Expert