Get post content from outside the loop
Is there a way to get the content from another outside the loop? The ID is 302 and I need to display the content of that on another page.
Is there a way to get the content from another outside the loop? The ID is 302 and I need to display the content of that on another page.
You must be logged in to post a comment.
You can use
get_page()
to return the$post
object of a static page:Edit
Similarly, you can use
get_post()
to return the$post
object of a post:to get the content of the post outside the loop you can write something like this
If your content include shortcodes, you should use:
Was looking for the same thing, I’m surprised nobody said this:
Works good! 🙂
For completeness, building on Tim’s comment above and inspired by Stephen Harris’s article, the solution that enables use of
the_content()
is:And hence filters get applied (paragraphs will be inserted etc.) and shortcodes work.
You can use, as said, the solution with
get_post
and$post_object->post_content
, but don’t forget to add a check before you use that post object:Since you know your target post ID (302), you may find useful this shorthand syntax that you can use out of the loop (though its performance is pretty much the same as in any other alternative method:)
You can use the
get_post_data()
function to get post outside the loop. Place this code in functions.phpand then add this snippet for more control on the process
You can simply call get_the_content(postId)
use
wp_reset_postdata();
it will work.. (edited)posttype can be “post” , “page” or your custom post type. Here p=302 is your post id.. Hope it will work.
you can put content in a category X and use query_post before while like this :