这是在基于子域的多站点上工作的,其中包含重新记录(广播或任何)的子帖子,这些子帖子没有原始内容(因为它从其父站点获取)。
-
添加到function.php:
function improved_trim_excerpt($text) {
global $post;
if ( '' == $text ) {
$text = get_the_content('');
$text = apply_filters('the_content', $text);
$text = strip_tags($text, '<a><img>');
$delim = '</a>';
$start = 0;
$items = 3;
$the_excerpt = implode($delim, array_slice(explode($delim, $text), $start,$items)) . $delim;
}
return $the_excerpt;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');
-
内容页的部分:
global $post;
$postid = ($id == $post->ID) ? $post->ID : $id;
$content = $post->post_content;
preg_match_all( '/(http:|https:)?\/\/([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?/', $content, $matches );
$count = 0;
if ( ! empty( $matches ) && ! empty( $matches[ 0 ] ) ) {
foreach ( $matches[ 0 ] as $url ) {
$split = explode( '#', $url );
$split = explode( '?', $split[ 0 ] );
$split = explode( '&', $split[ 0 ] );
$filename = basename( $split[ 0 ] );
$file_type = wp_check_filetype( $filename, wp_get_mime_types() );
if ( ! empty ( $file_type[ 'ext' ] ) ) {
if( ! in_array( $file_type[ 'ext' ], array('jpg', 'png', 'gif', 'webp', 'bmp')) ) continue;
$files[ $url ] = $file_type;
$urls[]=$url;
$count ++;
}
}
}
$no_of_photos = $count / 2;
$limit_photos = 3;
if( $no_of_photos <= $limit_photos ) {
the_content(); ?>
} else {
the_excerpt(); ?>
}
特别感谢@andreas:
https://stackoverflow.com/a/50609326/9565969
以及@jgraup:
https://wordpress.stackexchange.com/a/246063/134567