0

I would like to have my index or posts overview page load each post teaser starting at char 206 of each post's body content. This as all posts start with a general introduction in the post body content that is the same. To load that on the index.php teaser is useless information.

With get_the_post I managed to load content but the substr does not work as it should. It filter the content for the page. Not for the content of each page (body text).

Here is the code:

<?php 

/*
Template name: Blog Page
*/

get_header();
the_post();

?>

<div class="grid-container blog" id="main-container">

    <div class="grid-100 mobile-grid-100 nopadding" id="normal-content-wrap">

        <h1><?php the_title();?></h1>

        <?php 
        $content = apply_filters( 'the_content', get_the_content() );
        //$content = strip_tags($content);
        echo substr($content, 200, 20);
        echo $content;
        //the_content();?>

    </div>

</div>

<?php get_footer(); ?>

As you can see I commented out the strip_tags as I realized the formatting is fine. I do not want to loose the formatting. I just want each individual post to show a piece of that post starting at char x. How can this be done?

1

This should work fine

 if(have_posts()){
    while(have_posts()){
      the_post();
      $content = get_the_content();
      echo substr($content, 200, 20);
    }
 }
  • Well I tried that before and that loads nothing. The variable $content also shows length zero doing a var_dump – rhand Sep 26 '17 at 7:31
  • are you using the WP loop? – Piyush Rawat Sep 26 '17 at 7:33
  • No, see the code. It loaded the_content just fine without a loop there before. And that way it loads all post teasers. Using apply_filter it does allow me to use get_the_content, but it won't do. Please do share the complete code you would suggest then. Did try a loop before, but in vain.. – rhand Sep 26 '17 at 7:36
  • get_the_content() works inside the loop only. Check edited answer – Piyush Rawat Sep 26 '17 at 7:38
  • Yeah, and tried your code example before once, myself, but zero content is loaded this way I'm afraid. – rhand Sep 26 '17 at 7:41
1

You can try to do it without standard loop, just using global variables

global $post;
$content = apply_filters('the_content', $post->post_content); 
  • Well doing a $content = apply_filters('the_content', $post->post_content); echo $content; does get me to load the same and have the excerpt replace post content where an excerpt has been added so this seems to be a great option. However also just using the_content does the same too I realized. My initial approach was loading the post_content starting at char 204 and then a teaser. Perhaps I will stick to an excerpt there and be done with it. – rhand Sep 27 '17 at 4:15

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.