Get and Trim Full Post Content in WP Query

Question

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();
?>

--------------------------------------------------

>>> Share a link to this question, just copy and paste the code bellow on your web page <<<

<a href="https://www.wptricks.com/question/get-and-trim-full-post-content-in-wp-query/">

Get and Trim Full Post Content in WP Query</a>
0
, Summer Developer 4 months 0 Answers 33 views 0

Leave an answer