我的主题有以下两个功能:
function content($limit) {
global $post;
$content = explode(\' \', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content);
$content = wp_strip_all_tags($content, true);
} else {
$content = implode(" ",$content);
}
$content = preg_replace(\'/\\[.+\\]/\',\'\', $content);
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]>\', $content);
return $content;
}
function contentnoimg($limit) {
global $post;
$content = explode(\' \', get_the_content(), $limit);
if (count($content)>=$limit) {
array_pop($content);
$content = implode(" ",$content);
$content = wp_strip_all_tags($content, true);
} else {
$content = implode(" ",$content);
}
$content = preg_replace(\'/(<img.+?>)/\',\'\', $content);
$content = apply_filters(\'the_content\', $content);
$content = str_replace(\']]>\', \']]>\', $content);
return $content;
}
在循环中,它被称为:
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail( \'full\', array(\'class\'=>\'post_thumbnail_common\', \'alt\' => get_the_title() , \'title\' => get_the_title(), \'itemprop\'=>\'image\' ));
echo contentnoimg(41);} else { echo content(41); } ?>
我想网站显示
the_excerpt();
只有当它存在时。如果
has_excerpt()
是负数,则应按当前显示的内容工作
get_the_content()
代码应该是什么样子?我尝试了不同的方法,但我的网站停止工作,所以我做错了什么。
最合适的回答,由SO网友:WordPress Mike 整理而成
这是可行的。您可能只是有一个语法错误。
function content( $limit ) {
global $post;
if( has_excerpt() ){
$content = the_excerpt();
} else {
$content = explode( \' \', get_the_content(), $limit );
if ( count($content) >= $limit ) {
array_pop( $content );
$content = implode( " ", $content );
$content = wp_strip_all_tags( $content, true );
} else {
$content = implode( " ", $content );
}
$content = preg_replace( \'/\\[.+\\]/\',\'\', $content );
$content = apply_filters( \'the_content\', $content );
$content = str_replace( \']]>\', \']]>\', $content );
}
return $content;
}