首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WordPress:文本之后但图像之前的摘录

WordPress:文本之后但图像之前的摘录
EN

Stack Overflow用户
提问于 2014-09-01 02:13:27
回答 2查看 102关注 0票数 1

我想知道如何做到以下几点:

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

第一:我会对这个解决方案感到满意。2:在这种情况下,最好有一个可供选择的句子。“单击以查看图片。”

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

目前我有以下内容:

代码语言:javascript
复制
/*-----------------------------------------------------------------------------------*/
/* 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' );

任何帮助都是非常感谢的。

非常感谢和亲切的问候。

EN

回答 2

Stack Overflow用户

发布于 2014-09-01 03:27:31

我想,你可以自己写代码。你可以给the_content()添加滤镜,或者用另一种方法:参考这个:http://wordpress.org/support/topic/executing-a-function-only-if-a-post-contains-an-image,你可以检查the_content()是否有img?!对于计算the_content()中的单词,这个答案很有用:Counting words on a html web page using php

票数 0
EN

Stack Overflow用户

发布于 2014-09-03 06:12:11

我做了一些更深入的研究,得出了以下结论。我知道它不会工作,但有些部分是独立工作的,但是我不能把所有的东西都放在一起。也许有人能帮我指出正确的方向?

代码语言:javascript
复制
    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' );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25594868

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档