1

Simple task, but it isn't working:

<?php 
$content = apply_filters( 'the_content', get_the_content() );
$contentWithoutHTML = wp_strip_all_tags($content);
$pos = strpos($contentWithoutHTML, " ", 100);
$contentFinal = substr($contentWithoutHTML,0,$pos ); 
echo $contentFinal . "...";
?>

My post is way over 100 characters long (with spaces), and yet I get strpos(): Offset not contained in string error, which leads me to believe it isn't actually pulling the entire content string. But I applied filters like I believe I should... please assist. Also sometimes even if I get no offset error I just get ... even though again, over 100 characters with spaces... although sometimes it works. Why the inconsistency?

This is in a WP_Query loop in which most of them work, but some of them do not... so I am pretty sure it is being fed a string because I see it happening to the other posts in the loop...

Full Loop:

<?php
$args = array(
    'orderby' => 'ID',
    'order' => 'ASC',
    'posts_per_page' => 7,
    'meta_query' => array(
        array(
            'key' => '_thumbnail_id'
        )
    )
);
$query = new WP_Query($args);
while ($query->have_posts()):
    $query->the_post();
?>
                            <div class="articleboxs boxs">
                                <div class="col-sm-4 noleft">
                                    <div class="aricleleft boxs">
                                      <?php the_post_thumbnail('medium', array(
        'class' => 'img-responsive'
    )) ?>  
                                    </div>
                                </div>
                                <div class="col-sm-8 noright">
                                    <div class="aricleright boxs">
                                        <div class="boxs">
                                             <a href="<?php echo get_permalink(); ?>"><h2 class="heading font_Libre"><?php the_title(); ?></h2></a>
                                                <?php
    $content = apply_filters('the_content', get_the_content('', true));
    print_r($content);
    $contentWithoutHTML = wp_strip_all_tags($content);
    $pos = strpos($contentWithoutHTML, " ", 100);
    $contentFinal = substr($contentWithoutHTML, 0, $pos);
?>
                                 <p class="font_Roboto"><?php echo $contentFinal . "..."; ?></p>
                                        </div>
                                    </div>
                                </div>
                            </div>
                          <?php
endwhile;
wp_reset_postdata();
?>
0

I think the best thing to do would be to just simplify what you're doing. I'd just do something simple with it if all you're attempting to do is trim the amount of characters output. I'd use mb_strimwidth to trim the characters, and within that run wp_strip_all_tags() on get_the_content(). Like this:

$content = mb_strimwidth(wp_strip_all_tags(get_the_content()), 0, 100, '...');
echo $content;

Edit: Forgot to note that if you're going to have shortcodes in your content, which I highly recommend against, you may also have to wrap get_the_content() with strip_shortcodes().

  • My problem is that even $content = apply_filters( 'the_content', get_the_content('', TRUE) );print_r($content); doesn't print out the entire content for a handful of posts in my query... – Summer Developer Aug 1 '18 at 20:40
0

The answer to this question lies in the role of

get_the_content()

As it is now clear to me, this function is not designed to get the entirety of the post content in the loop, which is why part of the function talks about $more_link_text and $strip_teaser as arguments, in regards to the text that gets trimmed.

Furthermore, apply_filters('the_content', get_the_content('', true)) doesn't solve this fundamental problem, it merely does some HTML changes to the content.

So instead in my loop, I reference the global $post.

$post->post_content and do the strpos and substr from there.

Basically, as I am doing my own trimming/excerpt functionality, I can bypass get_the_content() entirely. As I am also wanting to put it right into a <p> tag, my code can just look like this:

<?php 
$contentWithoutHTML = wp_strip_all_tags($post->post_content);
$pos = strpos($contentWithoutHTML, " ", 100);
$contentFinal = substr($contentWithoutHTML,0,$pos ); 
?>
<p class="font_Roboto"><?php echo $contentFinal . "..."; ?></p>

Don't forget to reset the WP_Query after so as to keep leveraging $post sucessfully, with wp_reset_postdata(); after the loop.

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.