Differences between the_excerpt vs get_the_excerpt
This post tells about the_excerpt vs get_the_excerpt on the key differences that will help to know when and how to use get_the_excerpt and the_excerpt.
You may have been trying to show the excerpt of the post outside the loop but which function among these two functions to be called?
You should look out what the_excerpt does as well as get_the_excerpt.
the_excerpt is a WordPress function which displays the short summary of the post known as the excerpt but get_the_excerpt retrieves the excerpt of the blog and it passes one argument which is either an integer representing the number of a post or the WP_Post global object $post that allow retrieving other data of the post.
the_excerpt uses get_the_excerpt to set out the excerpt of the post after applying some filters using an apply_filters function.
Filters include things like changing the number of words of excerpts.
the_excerpt vs get_the_excerpt summary
# | the_excerpt | get_the_excerpt |
---|---|---|
1 | Displays the excerpt of the post without echoing the function | Needs an echo to output the excerpt of the post |
2 | Very useful to be called inside the loop | Very useful to be used outside the loop |
3 | It filters the post to display the excerpt | It does not filter the excerpt |
4 | It only returns the most current post excerpt if it is called outside the loop | It can return any excerpt from the post if the post number is provided as a parameter of the function. |
5 | It cannot show the modified date of the post | It can use the global $post object to display the date on which the post was changed. |
I can say these are just key differences between the_excerpt and get_the_excerpt that I learned.
But as I tested each function on the index.php, I found the usages are not the same in some cases.
Let us start with . . . . ,
the_excerpt usages
the_excerpt doesn’t receive any argument hence it is recommended to be implemented inside the while loop.
This is because its main work is to show the excerpt of the most current post, therefore, calling the_excerpt inside the loop will display the summaries of all posts of the blog.
This how the function is supposed to be invoked.
1. <?php if(has_posts()):?>
2. <?php while(has_posts()): the_post();?>
3.<?php the_excerpt();?>
The excerpt has been used on line number 3, let me explain each line in the way I understand.
Line 1 checks if the blog posts are available if it is true the while loop is executed, and the_post() is called.
Why the_post is used in this case?
It checks if the loop has started and then as the loop iterate the blog posts on line number 2, the_post will return the current post which is needed by the_excerpt function.
Therefore, in order for the_excerpt to list all the blog post excerpts a while loop and the_post() function are required.
Now, the question is what if you want to display the summary of the not current post only?
To do that you should get to know the get_the_excerpt function.
So, let’s check . . . . .,
get_the_excerpt usages
get_the_excerpt can be used to retrieve the excerpt from any post whether it is most the current one or not.
The function can display the post excerpt by echoing like this <?php echo get_the_excerpt();?>
If you don’t pass any argument get_the_excerpt returns the most current post only.
And in order to fetch all posts summaries, the function must be used inside the while loop.
This same as the_title vs get_the_title.
This is what I mean,
1. <?php if(has_posts()):?>
2. <?php while(has_posts()): the_post();?>
3.<?php echo get_the_excerpt();?>
Then, the usage is the same as how the_excerpt was implemented.
If you want to display the excerpt of a certain post but it is not a current one, get_the_excerpt offers the solution for this.
Just pass the ID of the post, the post id is the post number.
For example, when you want to output the excerpt of post number 3, the post id is 3.
Then, the code will be written as…
<?php echo get_the_excerpt(3);?>
But this should be written outside the loop otherwise the same description appears more than once.
Also, get_the_excerpt receives a WP_Post object which allows retrieving other data of the post.
The default object is a global $post object.
If you pass the object, get_the_excerpt must be revoked inside a loop like the_excerpt.
For example, if you want to show the parent post of each excerpt, then do the following.
<?php echo get_the_excerpt($post->
post_parent);
?>
When to use the_excerpt
Use the_excerpt when you want to display all the short descriptions of the blog posts.
It is useful when you want to filter the excerpt of the post like to increase the length of the excerpt words.
Although, this requires to write a custom function in the functions.php file like this
1.function edit_excerpt_length(){
2.return 75;
3.}
4.add_filter('excerpt_length','edit_excerpt_length');
This function brings the excerpt of 75 words.
There are so many ways to filter the excerpt in WordPress so check the contributors’ section in this link to see how to modify the_excerpt
When to use get_the_excerpt
Use get_the_excerpt when you want to embed the excerpt with other information like the last time when the post was edited.
And also, use it when you want to show the excerpt of a specific post but not a current post.
The more usages can be implemented if understand the members of the WP_Post object.
Wrap-up
In summary, the_excerpt loads the excerpt data using the get_the_excerpt function. The the_excerpt echoes the apply_filters function.
apply_filters passes two variables, which are the name of the function and the post excerpt returned by the get_the_excerpt() function.
Although, the function related to each other there are some differences as the_excerpt passes no argument.
But get_the_excerpt provides some flexibility to add more as it can receive WP_Post object.
Careful usages are needed for better practice.