限制摘录长度的自定义功能作为摘录(WP)的一部分返回帖子标题

时间:2012-02-28 06:47:26

标签: php wordpress function

我将以下内容添加到functions.php中:

function excerpt($limit) {
global $id;
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt).'... <p class="readmore"><a href="'. get_permalink($id) . '" title="' . the_title_attribute( array('echo' => 0, 'before' => 'Permalink to: ', 'after' => '')) . the_title() . '">Read More &#187;</a></p>';
  } else {
$excerpt = implode(" ",$excerpt).'... <p class="readmore"><a href="'. get_permalink($id) . '" title="' . the_title_attribute( array('echo' => 0, 'before' => 'Permalink to: ', 'after' => '')) . the_title() . '">Read More &#187;</a></p>';
  } 
  $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  return $excerpt;
}

由于某种原因,它将作为摘录的一部分返回帖子标题(帖子标题首先出现,然后在标题和摘录之间没有空格的摘录 - 此处示例:http://autisticadvocacy.uniongraphics.org/category/news/)。谁能告诉我我做错了什么?

2 个答案:

答案 0 :(得分:1)

$excerpt = implode(" ",$excerpt)
  . '... <p class="readmore"><a href="'
  . get_permalink($id)
  . '" title="'
  . the_title_attribute(array(
         'echo'   => 0, 
         'before' => 'Permalink to: ', 
         'after' => ''))
  ### change is in this line. (the_title() removed)
  . '">Read More &#187;</a></p>'
  ;

编写代码的方式非常难以阅读,因此在遇到问题时难以调试。尝试改善这一点。更好的格式化字符串的有用功能是sprintf()

$excerpt = sprintf('%s ... <p class="readmode"><a href="%s" title="%s">Read More &#187;</a></p>'
    , implode(" ",$excerpt), get_permalink($id)
    , the_title_attribute(array('echo' => 0, 'before' => 'Permalink to: ', 'after' => ''))
);

答案 1 :(得分:0)

将以下代码添加到functions.php文件中以获取摘录和内容。

<?php
// Custom Excerpt 
function excerpt($limit) {
$excerpt = explode(' ', get_the_excerpt(), $limit);
if (count($excerpt)>=$limit) {
array_pop($excerpt);
$excerpt = implode(" ",$excerpt).'...';
} else {
$excerpt = implode(" ",$excerpt);
} 
$excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
return $excerpt;
}

// Content Limit 
function content($limit) {
$content = explode(' ', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content).'...';
} else {
$content = implode(" ",$content);
} 
$content = preg_replace('/\[.+\]/','', $content);
$content = apply_filters('the_content', $content); 
$content = str_replace(']]>', ']]&gt;', $content);
return $content;
}
?>

现在,不要在循环中使用the_content()或the_excerpt,而是使用摘录($ limit)或内容($ limit)。

如果您想将摘录限制为300字,则代码如下所示:

<?php echo excerpt(50); ?>
<?php echo content(300); ?>

我有另一种方式来显示有限的字符摘录。这是functions.php文件代码。

function get_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 100);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
$excerpt = $excerpt.'... <a href="'.get_the_permalink().'">Read More</a>';
return $excerpt;
}

在此之后,您需要添加要按字符显示自定义角色的位置。

<?php echo get_excerpt(); ?>

来源:http://www.e2soft.com/blog/custom-excerpt-and-content-limit-wordpress/