Wordpress摘录-图像和文本
这是我的模型: 每一部分都是一篇带有特定标签的文章的摘录Wordpress摘录-图像和文本,wordpress,Wordpress,这是我的模型: 每一部分都是一篇带有特定标签的文章的摘录 有没有办法让客户端不必触摸、标记或编写这样的代码?您可以使用自己的功能过滤内容 与其使用它,不如使用它: 该函数可以放在functions.php上,您可以自由编写代码。您可以像这样使用内容过滤器 add_filter('the_content', 'my_content_filter'); // function my_content_filter($content) { global $post; if($post-
有没有办法让客户端不必触摸、标记或编写这样的代码?您可以使用自己的功能过滤内容 与其使用它,不如使用它: 该函数可以放在functions.php上,您可以自由编写代码。您可以像这样使用内容过滤器
add_filter('the_content', 'my_content_filter'); //
function my_content_filter($content) {
global $post;
if($post->post_excerpt == ''){ // check if the post has excerpt
$content = strip_tags($content); //strip tags
$cont_array = explode(' ',$content);
if(count($cont_array) > 55) //number of words wanted in excerpt default is 55
$content = implode(' ',array_slice($cont_array, 0, 55)).'...';
$content = '<p>'.$content.'</p>';
}else{
$content = $post->post_excerpt; //copy excerpt to content
}
return $content; //return content
}
上面的代码检查帖子是否有摘录,如果有摘录,则返回摘录,否则返回摘录的前55个单词默认长度。使用此代码限制帖子内容
<a href="<?php the_permalink(); ?>"><?php substr($post->post_content, 0, 12); ?> ...</a>
将其放在themes functions.php文件中:
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;
}
然后将其添加到循环中:
<?php print '<p>'.excerpt(40).'</p>'; ?>