gburning
gburning

Reputation: 504

Wordpress – Display page content before read more without using a loop

Confusing title, I know. Let me explain.

Currently, I have this little guy set up in my Wordpress theme:

<?php 
    $id=7; 
    $post = get_post($id); 
    $content = apply_filters('the_content', $post->post_content);
    echo $content;  
?>

What this does is it displays the content from a certain page of my choice.

Works great! Bravo! Fantastic! The only problem is that I'd like to only show the content before the read more-tag, not ALL of it. And that is where I'm at a loss.

My research (incessant googling, cursing and hammering om my keyboard) has lead me to understand that I could achieve this within a loop. But as I am only going to display the one page that seems a bit redundant.

Also, I'd rather not use the_excerpt as that means the end user won't be able to change the content later on.

Thanks for reading and I hope you can help! Cheerio!

Upvotes: 3

Views: 3416

Answers (2)

Marco Panichi
Marco Panichi

Reputation: 1185

By studying the array returned by the get_extended() function, I discovered that:

  • If the tag has been defined in the post, then:
    • $content_arr['main'] matches the text BEFORE the <!--more--> tag
    • $content_arr['extended'] matches the text AFTER the <!--more--> tag
  • Otherwise:
    • $content_arr['main'] matches the entire text of the post
    • $content_arr['extended'] is empty

Please note: This behavior differs significantly from what is reported in the documentation. Yet in my WordPress installations I have verified this behavior. This occurs whether the Settings > Reading > "For each post in a feed, include" setting is set to Full text or Excerpt

For this reason, the solution I would propose is the following:

$content_arr = get_extended( $post->post_content );
if( !empty($content_arr['extended']) )
{
    echo apply_filters( 'the_content', $content_arr['main'] );
}

Upvotes: 0

Use the WordPress function get_extended($post_content) to retreive text in content before the <!--more--> tag.

For example:

$id=7; 
$post = get_post($id); 
$content_arr = get_extended($post->post_content);
echo apply_filters('the_content', $content_arr['main']);

Upvotes: 5

Related Questions