douhanzhen8927 2014-05-06 15:09
浏览 18
已采纳

Wordpress短信博客Feed

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 );
?>
  • 写回答

3条回答 默认 最新

  • douhuanchi6586 2014-05-06 15:22
    关注

    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' );  
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探