Wordpress短信博客Feed

时间:2014-05-06 15:09:03

标签: php wordpress feed shortcode

我正在尝试使用短代码在主页(静态页面)上创建我的博客Feed。

到目前为止,我已设法让它显示标题,但我还希望它在每个显示的帖子标题下显示250个字符的条目内容。

换句话说,我只需要它来显示标题和前几句。

甚至可能吗?

这是用于创建Feed的代码。(它在标签内显示预定义数量的最新帖子标题)

 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;

解决 特别感谢pmandell和gtr1971。

完成feed.php代码,该代码可创建限制为100个字符的Feed。

<?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 个答案:

答案 0 :(得分:2)

是的,当然这是可能的。您正在使用WordPress循环,因此只需使用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;

您可以使用new_excerpt_more过滤器来控制摘录末尾的显示内容。这是一个示例,其中“[...]”显示在摘录的末尾,作为帖子的链接。

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

答案 1 :(得分:1)

您可以使用the_excerpt()功能并限制其长度。在Wordpress Codex中查看更多信息 Function Reference: the_excerpt

  

使用过滤器控制摘录长度:默认情况下,摘录长度为   设为55个字。使用的将摘录长度更改为20个单词   excerpt_length过滤器,将以下代码添加到functions.php文件中   你的主题。

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

答案 2 :(得分:0)

使用substr($ text,0,250)。这意味着您想要将字符串从位置0切割为250。