📜  wordpress post excerpt from post id (1)

📅  最后修改于: 2023-12-03 15:21:13.020000             🧑  作者: Mango

WordPress Post Excerpt from Post ID

WordPress is a popular content management system that allows website owners to easily manage their website's content. In this tutorial, we will show how to fetch a post's excerpt by using its post ID.

Overview

In WordPress, every post has a unique ID number. We can use this ID to fetch different aspects of the post, including its excerpt. The excerpt is a summary of the post's content, and it often appears on the homepage or in archive pages.

Getting the Post ID

Before we can fetch a post's excerpt, we need to know its post ID. There are different ways to get the post ID, but the easiest one is to go to the post editor in the WordPress admin area and look at the URL. The post ID is the number that comes after post= in the URL.

Fetching the Post Excerpt

Once we have the post ID, we can use the get_post_field() function to fetch the post excerpt. The get_post_field() function returns the value of a specific post field, such as the excerpt. Here's how to use it:

$post_id = 123; // Replace 123 with the actual post ID
$excerpt = get_post_field( 'post_excerpt', $post_id );

The code above will fetch the excerpt of the post with ID 123 and store it in the $excerpt variable. If the post doesn't have an excerpt, the $excerpt variable will be empty.

Conclusion

In this tutorial, we learned how to fetch a post's excerpt by using its post ID in WordPress. The get_post_field() function is a handy tool for accessing different parts of a post, and we can use it to get the excerpt, title, content, and more.