Top useful WordPress functions list and examples

WordPress functions list:

  1. the_post() 
  2. have_posts() 
  3. the_title() 
  4. get_the_title() 
  5. has_post_thumbnail() 
  6. the_post_thumbnail() 
  7. get_the_post_thumbnail() 
  8. the_content() 
  9. the_date() vs get_the_date( ) 
  10. the_time() vs get_the_time() 
  11. comments_number();  vs get_comments_number() 
  12. get_comments_number_text()   
  13. comments_template()  
  14. comments_open()  
  15. comment_date( ) vs get_comment_date()  
  16. comment_time( ) vs get_comment_time()  
  17. wp_list_comments()
  18. comment_author()
  19. comment_form()
  20. wp_dashboard_recent_comments()
  21. get_avatar()

the_post()

Check if the loop has started and configure the next post to be displayed with each iteration within the loop.

        
        <?php
if ( have_posts() ) :
    while ( have_posts() ) : the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <?php
        // Check if the post has a featured image
        if ( has_post_thumbnail() ) {
            the_post_thumbnail(); // Display the featured image
        }
        ?>
        <div><?php the_content(); ?></div>
        <p>Posted on: <?php the_date(); ?> at <?php the_time(); ?></p>
        <p>Comments: <?php comments_number(); ?></p>
        <p>Author: <?php the_author(); ?></p>
    <?php endwhile;
endif;
?>
        
    

the_date() vs get_the_date( )

the_date() function retrieves the date and display of the current post.

get_the_date( ) function is used to get the date of the current post and returns a string.


        
        <?php 
the_date();
the_date('F j, Y');

//display the custom date formate
 echo get_the_date(); 
echo get_the_date('F j, Y');
        
    

Related Post

the_time() vs get_the_time()

the_time() function retrieves the time and display of the current post.

get_the_time( ) function is used to get the time of the current post and returns a string.

        
        <?pph 
the_time();
echo  the_time('F j, Y g:i a');

echo get_the_time();
echo get_the_time('F j, Y g:i a');
?>
        
    

comments_number();  vs get_comments_number();

comments_number() function retrieves the total comment number of the current post and displays it.

get_comments_number( ) function is used to get the total  comment number of the current and returns a string.

        
        <?php
comments_number();

echo get_comments_number()
?>
        
    

get_comments_number_text()

This function is used to generate a human-readable string that describes the number of comments on a post.

        
        <?php
$comments_text = get_comments_number_text('No Comments', '1 Comment', '% Comments');
echo $comments_text;
?>
        
    

comments_template()

comments_template() function is used to include the comments template file in the current post single page. This function is always placed within the loop to display the comments section for a post.


        
        <?php
if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        // Your post content goes here
        the_title();
        the_content();

        // Include the comments template
        comments_template();

    endwhile;
endif;
?>
        
    

comments_open()

comments_open() function is used to check whether the comments are open or not in WordPress settings for a particular post or page. It returns a boolean value: true if comments are open, and false if comments are closed.

        
        <?php
if ( comments_open() ) {
    // Display your comments section or comment form here
} else {
    // Comments are closed, handle accordingly
}
?>
        
    

comment_date( ) vs get_comment_date()

comment_date() function retrieves and displays the comment date of the current comment.

get_comment_date( ) function is used to get the comment date of the current comment and returns a string.

        
        <div>Comment posted on <?php comment_date(); ?></div>
<div>Comment posted on <?php comment_date('M j, Y, g:i a'); // Mar 10, 2023, 6:10 pm ?></div>

<div>Comment posted on <?php echo  get_comment_date(); ?></div>
<div>Comment posted on <?php echo   get_comment_date('M j, Y, g:i a'); // Mar 10, 2023, 6:10 pm ?></div>

comment_date( ) vs get_comment_date()

Displays the comment date of the current comment.
        
    

comment_time( ) vs get_comment_time()

comment_time() function retrieves and displays the comment time of the current comment.

get_comment_time( ) function is used to get the comment time of the current comment and returns a string.

        
        <?php comment_time(); ?>

<?php
$comment_time = get_comment_time('U'); // 'U' returns the Unix timestamp
echo date('F j, Y g:i a', $comment_time);
?>
        
    

wp_list_comments()

The wp_list_comments() function is used to display a list of comments for the current post or page. It is used within the WordPress comments.php template file.
for more information visit
        
        <ol class="comment-list">
<?php wp_list_comments(); ?>
</ol>

// or we can pass the argument to the output comment
<?php
$args = array(
    'walker'            => null,
    'max_depth'         => '',
    'style'             => 'ol',  // or 'ul' for unordered list
    'callback'          => null,
    'end-callback'      => null,
    'type'              => 'all',
    'reply_text'        => 'Reply',
    'page'              => '',
    'per_page'          => '',
    'avatar_size'       => 32,
    'reverse_top_level' => null,
    'reverse_children'  => '',
    'format'            => 'html5',  // or 'xhtml' for XHTML doctype
    'short_ping'        => false,
    'echo'              => true,  // Whether to echo the list or return it
);

wp_list_comments($args);
?>
        
    

comment_author()

The comment_author() function retrieves and displays the name of the author for the current comment.

        
        <div>Comment by <?php comment_author(); ?>:</div>
        
    

Above is a traditional way to itrate loop  we can also run a loop using curly braces

        
        <?php
if ( have_posts() ){
    while ( have_posts() ) {
         the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <?php
        // Check if the post has a featured image
        if ( has_post_thumbnail() ) {
            the_post_thumbnail(); // Display the featured image
        }
        ?>
        <div><?php the_content(); ?></div>
        <p>Posted on: <?php the_date(); ?> at <?php the_time(); ?></p>
        <p>Comments: <?php comments_number(); ?></p>
        <p>Author: <?php the_author(); ?></p>
    <?php };
};
?>
        
    

comment_form()

The comment_form() function is used to display an HTML comment form inside a WordPress template file.

        
        <?php
$args = array(
    // Change the title of send button
    'label_submit' => __( 'Send', 'textdomain' ),
    // Change the title of the reply section
    'title_reply' => __( 'Write a Reply or Comment', 'textdomain' ),
    // Redefine your own textarea
    'comment_field' => '<div class="comment-fields"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label><br><textarea id="comment" name="comment" required> </textarea> </div>'
);

comment_form( $args ); ?>
        
    

wp_dashboard_recent_comments()

The wp_dashboard_recent_comments() function is used to output the list of recent comments in the WordPress dashboard. It is typically used in the dashboard to provide administrators with a quick overview of the latest comments on their site.

        
        <?php
// Display recent comments on the WordPress dashboard
wp_dashboard_recent_comments();
?>
        
    

get_avatar()

The get_avatar() function is used to fetch the avatar (profile picture) of a user. It's commonly used to display user avatars in comments, author boxes, or anywhere else user information is displayed.

        
        <?php echo get_avatar( get_the_author_meta( 'ID' ), 48 ); ?>
        
    

have_posts()

have_posts() function is often used in the context of a loop to check whether there are any posts to be displayed.

        
        <?php
if (have_posts()) {
    // There are posts to display
    while (have_posts()) {
        the_post();
        // ... Display post content here
    }
} else {
    // There are no posts to display
    // ... Display a message or alternative content
}
?>
        
    

the_title() 

the_title() function is used to display the title of the current post within the loop.

        
        <?php the_title(); ?>
        
    

get_the_title()

The get_the_title() function retrieves the title of the current post and returns a string in the loop.

        
        <?php echo get_the_title(); ?>
        
    

has_post_thumbnail()

has_post_thumbnail() function is used to check if the current post has a (post thumbnail (featured image)

        
        if ( has_post_thumbnail() ) {
    the_post_thumbnail();
}
else {
    echo '<img src="' . get_template_directory_uri( ) . '/images/default-thumbnail.jpg">';
}
        
    

the_post_thumbnail()

This function is used to display the feature image  or thumbnail image in blog or shop page

        
        <?php the_post_thumbnail();  ?>

<?php
//Default WordPress
the_post_thumbnail( 'thumbnail' );     // Thumbnail (150 x 150 hard cropped)
the_post_thumbnail( 'medium' );        // Medium resolution (300 x 300 max height 300px)
the_post_thumbnail( 'medium_large' );  // Medium Large (added in WP 4.4) resolution (768 x 0 infinite height)
the_post_thumbnail( 'large' );         // Large resolution (1024 x 1024 max height 1024px)
the_post_thumbnail( 'full' );          // Full resolution (original size uploaded)

//With WooCommerce
the_post_thumbnail( 'shop_thumbnail' ); // Shop thumbnail (180 x 180 hard cropped)
the_post_thumbnail( 'shop_catalog' );   // Shop catalog (300 x 300 hard cropped)
the_post_thumbnail( 'shop_single' );    // Shop single (600 x 600 hard cropped)
        
    

get_the_post_thumbnail()

It retrieves the post thumbnail  and returns string if you want to display you have to echo it

        
        <?php echo get_the_post_thumbnail();  ?> 

<?php
echo get_the_post_thumbnail( get_the_id(), 'thumbnail', array( 'class' => 'align-right' ) );
 ?>
        
    

the_content()

the_content() function is used to display the post content  inside the loop

        
        <?php the_content(); ?>
        
    

Leave a comment