【问题标题】:Wordpress shortcode blog feedWordpress 简码博客提要
【发布时间】:2014-05-06 15:09:02
【问题描述】:

我正在尝试使用简码在主页(静态页面)上创建我的博客提要。

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

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

有可能吗?

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

 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 个字符的提要。

<?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 );
?>

【问题讨论】:

    标签: php wordpress feed shortcode


    【解决方案1】:

    是的,这当然是可能的。您正在使用 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' );  
    

    【讨论】:

    • 这很好,但是我仍然缺少阅读更多按钮,我想这是一个主题问题。有什么解决方法吗?
    【解决方案2】:

    您可以使用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 );
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2011-01-22
        • 1970-01-01
        • 2012-08-02
        • 1970-01-01
        • 2014-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-07-26
        相关资源
        最近更新 更多