WordPress:摘录在文本之后,但在图像之前


WordPress: Excerpt after text but before images

我想知道如何执行以下操作:

我想在45个字之后摘录一段,但如果帖子的文本少于45个字,并且帖子中包含了图像,那么应该在文本后面包含更多的标签。

第一:我对这个解决方案很满意。第二:在这种情况下,最好有一个替代句子,例如"点击查看图片"。

希望这对任何读到这篇文章的人都有意义。

目前我有以下几种:

/*-----------------------------------------------------------------------------------*/
/* Sets the post excerpt length to 15 characters.
/*-----------------------------------------------------------------------------------*/
function moka_excerpt_length( $length ) {
    return 45;
}
add_filter( 'excerpt_length', 'moka_excerpt_length' );

/*-----------------------------------------------------------------------------------*/
/* Returns a "Continue Reading" link for excerpts
/*-----------------------------------------------------------------------------------*/
function moka_excerpt_more( $more ) {
    return '&hellip; <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . __( 'Read more', 'moka' ) . '</a>';
}
add_filter( 'excerpt_more', 'moka_excerpt_more' );

非常感谢您的帮助。

非常感谢和问候。

我认为,您可以自己编写代码。您可以将筛选器添加到_content()或其他方式:请参阅:http://wordpress.org/support/topic/executing-a-function-only-if-a-post-contains-an-image,您可以检查_content()是否有img?!对于计算_content()中的单词,这个答案很有用:使用php 计算html网页上的单词

我做了一些更深入的研究,得出了以下结论。我知道它不起作用,但有些部分是独立工作的,但我无法将所有内容整合在一起。也许有人能帮我指明正确的方向?

    function individual_excerpt_more( $more ) {
if {
    function word_count() {
        $content = get_post_field( 'post_content', $post->ID );
        $word_count = str_word_count( strip_tags( $content ) );
        return $word_count; <45 // (less than 45 words)
        }
    && $content = $post->post_content;
    if( has_shortcode( $content, 'gallery', 'video' ) ) {
    // The content has a [gallery] & [video] short code, so this check returned true.
    }
    return '&hellip; <ins><a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read more</a></ins>';
    }
else {
    function theme_excerpt_length( $length ) {
        return 45;
    }
    add_filter( 'excerpt_length', ’theme_excerpt_length' );
}
add_filter( 'excerpt_more', 'individual_excerpt_more' );