Question

I am trying to create my blog feed on a home page(static page) using a shortcode.

So far I have managed to make it display titles, but I would also like it to display like 250 characters of the entry content under each displayed post title.

In other words, I just need it to display title and first few sentences.

Is it even possible?

This is the code used to create the feed.(it displays predefined number of latest posts titles inside tag)

 function getblogposts($atts, $content = null) {
       extract(shortcode_atts(array(
          'posts' => 1,
       ), $atts));

   $return_string = '<h3>'.$content.'</h3>';
   $return_string .= '<ul>';
   query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
   if (have_posts()) :
      while (have_posts()) : the_post();
         $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
      endwhile;
   endif;
   $return_string .= '</ul>';

   wp_reset_query();
   return $return_string;

SOLVED Special thanks to pmandell and gtr1971.

Complete feed.php code which creates feeds limited to 100 characters.

<?php

function getblogposts($atts, $content = null) {
   extract(shortcode_atts(array(
      'posts' => 1,
   ), $atts));

   $return_string = '<h3>'.$content.'</h3>';
   $return_string .= '<ul>';
   query_posts(array('orderby' => 'date', 'order' => 'DESC' , 'showposts' => $posts));
   if (have_posts()) :
      while (have_posts()) : the_post();
    $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a>';
    $return_string .= '<div class="excerpt">' . get_the_excerpt() . '</div></li>';
endwhile;
   endif;
   $return_string .= '</ul>';

   wp_reset_query();
   return $return_string;
}
function new_excerpt_more( $more ) {
    return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">[...]</a>';
}   
add_filter( 'excerpt_more', 'new_excerpt_more' );  


function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 100 );
?>
Was it helpful?

Solution

Yes, of course this is possible. You are using a WordPress loop, so just use get_the_excerpt().

while (have_posts()) : the_post();
    $return_string .= '<li><a href="'.get_permalink().'">'.get_the_title().'</a>';
    $return_string .= '<div class="excerpt">' . get_the_excerpt() . '</div></li>';
endwhile;

You can use the new_excerpt_more filter to control what displays at the end of the excerpt. Here is an example where "[...]" displays at the end of the excerpt, as a link to the post.

function new_excerpt_more( $more ) {
    return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">[...]</a>';
}   
add_filter( 'excerpt_more', 'new_excerpt_more' );  

OTHER TIPS

You can use the the_excerpt() function and limit its length. Look here in the Wordpress Codex for more info Function Reference: the_excerpt

Control Excerpt Length using Filters: By default, excerpt length is set to 55 words. To change excerpt length to 20 words using excerpt_length filter, add the following code to functions.php file in your theme.

function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

use substr($text, 0, 250). It means You want to cut the string from position 0 to 250.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top