0

I dont want to show image in my read_more function in homepage. read_more shows first 25 strings of my content. when I add image in the content it shows with the string inline. I don't want the image. Anyone know how to do it? here is my code below..

<div id="leadnewsbox" class="col-md-5 col-sm-4 col-xs-12">
<?php 
$breakingcat = get_the_category_by_id($btimes['breaking-news-category']);
$breakingnews = new WP_Query(array(
    'post_type' => 'post',
    'posts_per_page' => 1,
    'category_name' => $breakingcat
));
while($breakingnews->have_posts()) : $breakingnews->the_post(); ?>
    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('post-image'); ?></a>
    <div class="leadnewsboxtitle">
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <p><?php read_more(25); ?>...</p>
    </div>
<?php endwhile; ?>

// read_more function
function read_more($limit){
    $content = explode(' ', get_the_content());
    $less_content = array_slice($content, 0 , $limit);
    echo implode (' ', $less_content);
}
2
  • Looks like you are using a theme with custom functions in the loop. Need to see the code behind read_more(). This likely calls the_excerpt() for displaying the partial. It's possible to filter that output and remove an <img> tag before output. – jdm2112 Jan 20 '17 at 19:03
  • How can i remove the image sir? – Mukut Jan 20 '17 at 19:07
0

If I understand your question right, a function that truncates your text after stripping HTML is what you need.

What about using wp_trim_words():

<?php while($breakingnews->have_posts()) : $breakingnews->the_post(); ?>
    <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('post-image'); ?></a>
    <div class="leadnewsboxtitle">
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
        <p><?php echo wp_trim_words( get_the_content(), 60, '...' ); ?></p>
    </div>
<?php endwhile; ?>

General usage:

wp_trim_words( string $text, int $num_words = 55, string $more = null )
2
  • Thanks for your answer it works. but I used ream_more function so many times mistakenly. I don't want to change it. I found another solution like get_the_content from get_the excerpt and its working properly. thanks again man. You people are too much helpful. – Mukut Jan 21 '17 at 5:55
  • You can keep it: function read_more($limit) { echo wp_trim_words( get_the_content(), $limit, '&hellip;' ); } – Daniel Sixl Jan 21 '17 at 16:15
0

Here is the solution I found without changing the function.

// read_more function
function read_more($limit){
 $content = explode(' ', get_the_excerpt());
 $less_content = array_slice($content, 0 , $limit);
 echo implode (' ', $less_content);
}   

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.