5

I'm working on tweaking a theme and I need to explode the post content to remove some text. The following code is what I am using:

$custom_Get_Post_Title = explode('|',get_post()->post_content);
echo "<h4>" . $custom_Get_Post_Title[0] . "</h4>";              
echo "<p>" . $custom_Get_Post_Title[1] . "</p>";

Originally, the developer was using the follow code to display the post content, but it was making it hard for me to explode the data.

global $post;
setup_postdata($post);
the_content();

MY QUESTION: What is the difference between these two methods that both retrieve the post content?

6

post_content is a property of the WP_Post object. WP_Post is an object representing the post data from the database. So post_content contains the raw content as stored in the database.

the_content() is a template tag that displays the content of the current post. The 'current post' is whatever the global $post variable is set to at the time the function is run. The global $post variable is normally set within The Loop with while( have_posts() ) : the_post();.

The crucial difference is that the_content() runs the raw content through several filters that prepare it for display. These do things like adding paragraph tags, converting URLs to embeds for things like videos, and converting symbols like quotes to smart quotes etc. Many plugins use this filter as well, for adding things like Share buttons.

So if you just echo the post_content it probably won't look right. You can mimic the output of the_content() on raw data by applying the the_content filter manually. So in your example you would do:

$post = get_post();

$split_content = explode( '|', $post->post_content );

echo '<h4>' . $split_content[0] . '</h4>';              
echo apply_filters( 'the_content', $split_content[1] );
| improve this answer | |
3

get_post() is the same as global $post

functions such as the_content() can only be used inside of a loop.

the setup_postdata() function can be used to make those functions available.

Your top example could be rewritten as follows:

gloabl $post;
$custom_Get_Post_Title = explode('|',$post->post_content);

or you could do something similar with the bottom example:

global $post;
// setup_postdata($post); <- not really needed
$theContent = get_the_content($post->ID); // can used post-id to retrieve specific
$custom_Get_Post_Title = explode('|',get_post()->post_content);
| improve this answer | |

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.