Support » Developing with WordPress » How I get few words of a Post

  • How can I get the first few words of a post that does not have excerpt?

    $args = array(
        'posts_per_page'   => 5,
        'offset'           => 0,
        'category'         => '',
        'category_name'    => 'Energy',
        'orderby'          => 'date',
        'order'            => 'DESC',
        'include'          => '',
        'exclude'          => '',
        'meta_key'         => '',
        'meta_value'       => '',
        'post_type'        => 'post',
        'post_mime_type'   => '',
        'post_parent'      => '',
        'author'       => '',
        'author_name'      => '',
        'post_status'      => 'publish',
        'suppress_filters' => true
    );
    $posts_array = get_posts( $args );
    
     foreach($posts_array as $postCategory)
    {
      echo get_the_title( $postCategory->ID );
      echo get_permalink( $postCategory->ID );
      echo get_the_excerpt( $postCategory->ID ); // THIS WITH OU WITHOUT EXCERPT DOES NOT WORK
    }

    The code is printing the title and the permalink but it does not print the first few words of the post.

    I would like to understand how to print the first few words of the post, whether or not it has the excerpt delimiter.

    Thanks

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    I’d dump out the ID you’re passing to the function.

    add

    echo '<pre>';
    print_r( $postCategory );
    echo '</pre>';

    in your foreach.

    Hello @gislef,

    Please use below function it has 4 parameters you can limit the post content by entering the word_limit.

    It will show the number of words whether it has a excerpt or not.

    function get_the_post_excerpt( $post_id = null, $content = '', $word_limit = '55', $more = '...' ) {
    
    	$has_excerpt 	= false;
    	$word_length 	= !empty($word_length) ? $word_length : '55';
    
    	// If post id is passed
    	if( !empty($post_id) ) {
    		
    		if (has_excerpt($post_id)) {
    
    			$has_excerpt 	= true;
    			$content 		= get_the_excerpt();
    		} else {
    			$content = !empty($content) ? $content : get_the_content();
    		}
    	}
    
    	$content = wp_trim_words( $content, $word_limit, $more );
    
    	return $content;
    }
    • This reply was modified 1 year, 11 months ago by  Steve Stern. Reason: put code in backticks
    • This reply was modified 1 year, 11 months ago by  Steve Stern.

    @egf, @sterndata and @janak007

    thank you all for the answers

    @janak007 see below how I tried:

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       <div class="col-md-12">
          <a href="<?php the_permalink() ?>">
             <img class="img-responsive center-block" src="<?php the_post_thumbnail_url() ?>"></a>
          <a href="<?php the_permalink() ?>">
                <h2 class="name text-center"><?php the_title(); ?></h2>
          </a>
          <P class="name"><?php get_the_post_excerpt(); ?></P>
       </div>
    <?php endwhile;	?>

    How should I pass some parameter in the code below?

    <?php get_the_post_excerpt(); ?>

    Moderator bcworkz

    (@bcworkz)

    If 55 words are acceptable, you don’t need to pass anything because you are in the Loop. You can also alter the function declaration to default to another number. Change both instances of 55 to your desired number.

    You can specify a different number without changing the default by passing default values for parameters prior to the $word_limit parameter. Pass the number desired, for example to get a 22 word excerpt:
    <?php get_the_post_excerpt( null, '', 22 ); ?>

    Thanks @bcworkz

    <?php 
       $data = get_the_post_excerpt( null, '', 22, '...' );
       var_dump($data); 
    ?>

    anyway I tried to return empty

    return string(0) ""

    Moderator bcworkz

    (@bcworkz)

    Ah yes, my bad, my advice was based on a quick read of Janak’s code, it’s not as flexible as I had assumed. Please add the following above the wp_trim_words() line to improve flexibility:

    	if ('' == $content && empty( $post_id )) {
    		$content = get_the_content();
    	}
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How I get few words of a Post’ is closed to new replies.