dav*_*ker 63 php wordpress function
正如它在标题中所说,我正在寻找WordPress中的多个摘录长度.
我知道你可以在functions.php中做到这一点:
function twentyten_excerpt_length( $length ) {
return 15;
}
add_filter( 'excerpt_length', 'twentyten_excerpt_length' );
Run Code Online (Sandbox Code Playgroud)
我想知道的是你如何让这些中的多个返回不同的数值,这样我就可以得到侧边栏循环的简短摘录,特色循环的更长摘录,以及主要文章的最长摘录.
在模板中使用这些东西:
<?php the_excerpt('length-short') ?>
<?php the_excerpt('length-medium') ?>
<?php the_excerpt('length-long') ?>
Run Code Online (Sandbox Code Playgroud)
干杯,戴夫
Mar*_*rty 95
怎么样...
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;
}
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(']]>', ']]>', $content);
return $content;
}
Run Code Online (Sandbox Code Playgroud)
然后在您的模板代码中,您只需使用..
<?php echo excerpt(25); ?>
Run Code Online (Sandbox Code Playgroud)
来自:http://bavotasan.com/tutorials/limiting-the-number-of-words-in-your-excerpt-or-content-in-wordpress/
Mic*_*bak 71
至于现在,你可以升级Marty的回复:
function excerpt($limit) {
return wp_trim_words(get_the_excerpt(), $limit);
}
Run Code Online (Sandbox Code Playgroud)
您还可以通过以下方式定义自定义"read more"链接:
function custom_read_more() {
return '... <a class="read-more" href="'.get_permalink(get_the_ID()).'">more »</a>';
}
function excerpt($limit) {
return wp_trim_words(get_the_excerpt(), $limit, custom_read_more());
}
Run Code Online (Sandbox Code Playgroud)
Bay*_*ae' 26
这就是我提出的.
将此添加到您的 functions.php
class Excerpt {
// Default length (by WordPress)
public static $length = 55;
// So you can call: my_excerpt('short');
public static $types = array(
'short' => 25,
'regular' => 55,
'long' => 100
);
/**
* Sets the length for the excerpt,
* then it adds the WP filter
* And automatically calls the_excerpt();
*
* @param string $new_length
* @return void
* @author Baylor Rae'
*/
public static function length($new_length = 55) {
Excerpt::$length = $new_length;
add_filter('excerpt_length', 'Excerpt::new_length');
Excerpt::output();
}
// Tells WP the new length
public static function new_length() {
if( isset(Excerpt::$types[Excerpt::$length]) )
return Excerpt::$types[Excerpt::$length];
else
return Excerpt::$length;
}
// Echoes out the excerpt
public static function output() {
the_excerpt();
}
}
// An alias to the class
function my_excerpt($length = 55) {
Excerpt::length($length);
}
Run Code Online (Sandbox Code Playgroud)
它可以像这样使用.
my_excerpt('short'); // calls the defined short excerpt length
my_excerpt(40); // 40 chars
Run Code Online (Sandbox Code Playgroud)
这是我所知道的添加过滤器的最简单方法,可以从一个函数调用.
小智 10
我也在寻找这个功能,这里的大多数功能都很好而且灵活.对于我自己的情况,我一直在寻找一种解决方案,只在特定页面上显示不同的摘录长度.我正在使用这个:
function custom_excerpt_length( $length ) {
return (is_front_page()) ? 15 : 25;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
Run Code Online (Sandbox Code Playgroud)
将此代码粘贴到主题functions.php文件中.
小智 5
回到Marty的回复:
我知道自从这个回复发表以来已经有一年多了,但是迟到总比没有好.为了使用超过WordPress默认值55的限制,您需要替换此行:
$excerpt = explode(' ', get_the_excerpt(), $limit);
Run Code Online (Sandbox Code Playgroud)
用这一行:
$excerpt = explode(' ', get_the_content(), $limit);
Run Code Online (Sandbox Code Playgroud)
否则,该功能仅适用于已修剪过的文本.
您可以在您的functions.php文件中添加此功能
function custom_length_excerpt($word_count_limit) {
$content = wp_strip_all_tags(get_the_content() , true );
echo wp_trim_words($content, $word_count_limit);
}
Run Code Online (Sandbox Code Playgroud)
然后在你的模板中调用它
<p><?php custom_length_excerpt(50); ?>
Run Code Online (Sandbox Code Playgroud)
该wp_strip_all_tags应防止杂散html标签断裂的页面.
有关功能的文档
| 归档时间: |
|
| 查看次数: |
75449 次 |
| 最近记录: |