get_post() (get an article)
The get_post() function can query the information of an article based on the ID, and can also return the current article in the loop.
usage
get_post( $post, $output, $filter );
parameter
$id
(Integer | Object) (Optional) Article ID or article object, if empty, it will automatically be set to the current article.
Default value: null (current article)
$output
(String) (optional) Returns the form of the result, optional:
- OBJECT: Return an article object
- ARRAY_A: Return an array with key values
- ARRAY_N: Returns an array without key values
- Default value: OBJECT
$filter
(String) (Optional) Article information filtering method. For specific reference, please refer to the sanitize_post_field() function.
Default value: row
Return value
(Object | null | array) Returns the article object, array, or null.
example
Get an article with ID 7 and print out its title:
$post_7 = get_post( 7 ); $title = $post_7->post_title;
Get the article with ID 7 (array form) and print out its title:
$post_7 = get_post( 7, ARRAY_A ); $title = $post_7['post_title'];
other
This function is located in: wp-includes/ and wp-includes/
Get a category link
In WordPress development, it is often necessary to obtain links to categories.
If you know the classification ID, you only need to use the get_category_link() function to get it directly.
But in actual situations, you may only know a little bit of classified information. Let me introduce the method of obtaining classified links through various classified information.
Get the category link by category ID
It is simple to obtain a classification link based on the ID, just use the get_category_link() function.
echo get_category_link( 23 );
Getting a classification link according to the category alias requires one more step. First use the get_category_by_slug() function to get the classification based on the alias, and then get the classification link.
echo get_category_link( get_category_by_slug( 'tips' ) );
Get the category link based on the category name
Getting a classification link based on a classification name is similar to getting a classification based on a classification name. Both first get the classification and then get the link.
echo get_category_link( get_cat_ID( 'WordPress Tutorial' ) );