A wordpress development place
Hand-coded development for conversion into Wordpress
Different ways to Get post content outside the loop
When you need any post/page content inside a loop of different post/page or outside any loop you don’t need to access these with a database query. Using the WordPress’s in-build functions will do this for you. These functions are doing the same thing internally, like querying into database and outputting the results. It saves a lot of time for you.
Probably you are beginner of WordPress theme development and stuff you learn in the article will be very useful for you.
In this article first of all, we discuss different cases where you need to get a post or page contents outside or inside a loop. Therefore, you can access the title, the content, featured image , custom field values , etc. Furthermore. we discuss some WordPress functions that will help you to get your desired output. Finally, some techniques to get things done.
When you know the POST ID or PAGE ID or CUSTOM POST ID:
$post_info=get_post( 10 ); $post_title=$post_info->post_title; $post_content=$post_info->post_content;
The get_post() function gives more output than the title and content. See all the available outputs you can simply pass the post _info to print_r() :
Now you can see all the outputs given:
ID : post-id
post_name: the slug associated
post_date: date published
post_date_gmt: date in gmt
post_excerpt: excerpt(of custom field)
post_status: status(published,draft, pending review)
comment_status: status( open, closed )
ping_status: ping status( open, closed )
post_password: password if protected with password and empty if not protected
post_parent: id of post parent
post_modified: timestamp of last modified
post_modified_gmt: timestamp of last modified in gmt
comment_count: number of comments
If you know only the slug then you can get the ID with the following code:
$post = get_page_by_path('my_slug',OBJECT,'post'); $post_id=$post->ID;
How to get the featured image/categories associated/tags/taxonomies/custom fields values etc based on ID ?
How to Get the post thumbnail:
$post_thumbnail_url = get_the_post_thumbnail_url( $post_id, 'thumbnail' ); // you can specify the image size here.
The taxonomy terms :
$terms = get_the_terms( $post_id, 'slug' );
$categories = get_the_category( $post_id);