2

I was trying to get first 100 words of the content in the header, and I use the following snippet to get the first 100 words in the loop, but is it possible to get the value outside the loop:

$cstring = get_the_content( '' );
$newcString = substr( $cstring, 0, 100 );
echo'<p>' . $newcString . '</p>';

3 Answers 3

6

If you are trying to do it for the current page you are on you can just use this:

global $post;
$content = $post->post_content;`

This will get the content for the current post instead of having to set the ID specifically.

7

I wrote an article on this here, but here's a summary of the points:

  • the_content can only be used 'inside the loop'
  • 'inside the loop' can only be correctly 'simulated' by calling setup_postdata() and global $post.
  • You then need to clear up after yourself by calling wp_reset_postdata()

The below code provides a function to retrieve post content from the post ID. It differs from @NickYoung answer in that the content you receive is not what's stored in the post_content column of the posts table, but rather that content after it's been through the_content filter (e.g. shortcodes parsed etc).

The code

/**
 * Display the post content. Optinally allows post ID to be passed
 * @uses the_content()
 * @link http://stephenharris.info/get-post-content-by-id/
 * @link https://wordpress.stackexchange.com/questions/142957/use-the-content-outside-the-loop
 * @param int $id Optional. Post ID.
 * @param string $more_link_text Optional. Content for when there is more text.
 * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
 */
function sh_the_content_by_id( $post_id=0, $more_link_text = null, $stripteaser = false ){
    global $post;
    $post = get_post($post_id);
    setup_postdata( $post, $more_link_text, $stripteaser );
    the_content();
    wp_reset_postdata( $post );
}
4
  • That is a great point that it is not filtered. In that case you could use $post->post_content_filtered I think, right? I have never used that version before but it sounds like it would also do the trick.
    – Nick Young
    May 5, 2014 at 21:14
  • Way to go. $post->post_content doesn't go through filters
    – bysanchy
    Oct 26, 2016 at 4:38
  • This should be the accepted answer.
    – Christine Cooper
    Mar 20, 2018 at 12:27
  • wp_reset_postdata( ) does not accept any parameter. Maybe it did, I don't know. Jul 24, 2019 at 1:34
0

You can use get_page() or get_post() to get content out side the loop

//For page
$page_id = 1;
$get_page_object = get_page( $page_id );
$page_object = $get_page_object->post_content;
echo $newpagecString = substr($page_object, 0, 100);
//For post
$post_id = 2;
$get_post_object = get_post( $post_id );
$post_object = $get_post_object->post_content;
echo $newpostcString = substr($post_object, 0, 100);
5
  • And why there's $get_page_object when we are not calling it anywhere? Apr 30, 2014 at 20:13
  • I have updated my answer.Please check now
    – Maidul
    Apr 30, 2014 at 20:15
  • And $post_id = 2; is the ID of current post? Can we call it with $post_id = $wp_query->post->ID; ? Apr 30, 2014 at 20:16
  • $post_id is the id of that post from which you want to retrieve data.$wp_query->post->ID; will work on the loop.But i think you wanted to get content of a page or post form outside the loop.
    – Maidul
    Apr 30, 2014 at 20:21
  • Yea, but it's still not working in my case. Let me see what is wrong with my code. Apr 30, 2014 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.