Home >Backend Development >PHP Tutorial >Function usage in WordPress for obtaining article information and category links
get_post() (Get an article) The
get_post() function can query the information of an article based on its ID, and can also return the current article in the loop.
Usage
get_post( $post, $output, $filter );
Parameters
$id
(integer | object) (optional) article ID or article object, if empty, it is automatically set to the current article.
Default value: null (current article)
$output
(string) (optional) Return result form, optional:
$filter
(String) (optional) Article information filtering method. Please refer to the sanitize_post_field() function for details.
Default value: row
Return value
(Object | null | Array) Returns the article object, array or null.
Example
Get the 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 (in array form) and print out its title:
$post_7 = get_post( 7, ARRAY_A ); $title = $post_7['post_title'];
Others
This function is located at: wp-includes/post.php and wp-includes/class-wp-atom-server.php
Get category links
In WordPress development, it is often necessary to obtain category links.
If you know the category 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 classification information. Next, I will introduce the method of obtaining classification links through various classification information.
Get the category link based on the category ID
Getting the category link based on the ID is relatively simple, just use the get_category_link() function directly.
echo get_category_link( 23 );
Get the category link based on the category name
Getting the category link based on the category name requires one more step. First use the get_category_by_slug() function to get the category based on the alias, and then get the category link.
echo get_category_link( get_category_by_slug( 'tips' ) );
Get the category link based on the category name
Getting the category link based on the category name is similar to getting the category link based on the category name. Both get the category first and then get the link.
echo get_category_link( get_cat_ID( 'WordPress 教程' ) );
The above introduces the usage of functions in WordPress for obtaining article information and category links, including relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.