【问题标题】:WordPress: Excerpt after text but before imagesWordPress:文本之后但图像之前的摘录
【发布时间】:2014-10-25 00:50:35
【问题描述】:

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

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

1st:我对这个解决方案很满意。 第二:在这种情况下有一个替代句子可能很棒,例如。 “点击查看图片。”。

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

目前我有以下:

/*-----------------------------------------------------------------------------------*/
/* 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' );

非常感谢任何帮助。

非常感谢和亲切的问候。

【问题讨论】:

  • 您需要查看您的主题模板文件。如果要编辑 index.php 的摘录,则可以在该页面上执行 if / else。
  • 好的,谢谢您的提示。 if /else 会是什么样子?

标签: php wordpress


【解决方案1】:

我认为,您可以自己编写代码。 您可以将过滤器添加到 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

【讨论】:

  • 感谢您的回答。不幸的是,我自己无法编写代码。我的知识还不够基础。我只是想增强Wordpress更多链接,检查帖子中单词少于45时是否包含图片。
【解决方案2】:

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

    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' );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    相关资源
    最近更新 更多