Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文字后但图片前的摘录_Php_Wordpress - Fatal编程技术网

Php 文字后但图片前的摘录

Php 文字后但图片前的摘录,php,wordpress,Php,Wordpress,我想知道如何做到以下几点: /*-----------------------------------------------------------------------------------*/ /* Sets the post excerpt length to 15 characters. /*-----------------------------------------------------------------------------------*/ function

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

/*-----------------------------------------------------------------------------------*/
/* 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' );
我想在45个单词之后做一个摘录,但是如果文章的文本少于45个单词,并且文章中包含了图像,那么应该在文本之后包含more标记

1:我很乐意接受这个解决方案。 第二:在这种情况下,最好用另一句话,例如“点击查看图片”

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

目前我有以下几点:

/*-----------------------------------------------------------------------------------*/
/* 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' );
/*-----------------------------------------------------------------------------------*/
/*将文章摘录长度设置为15个字符。
/*-----------------------------------------------------------------------------------*/
函数moka_摘录_长度($length){
返回45;
}
添加过滤器('extract_length'、'moka_extract_length');
/*-----------------------------------------------------------------------------------*/
/*返回摘录的“继续阅读”链接
/*-----------------------------------------------------------------------------------*/
函数moka_摘录_more($more){
返回“&hellip;”;
}
添加过滤('extract_more'、'moka_extract_more');
非常感谢您的帮助


非常感谢和亲切的问候。

我想,您可以自己编写代码。 您可以将筛选器添加到_content()或其他方式: 请参考以下内容:,您可以检查_content()是否有img?! 对于计算内容()中的单词,这个答案很有用:

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

    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' );
function-individual\u-extract\u-more($more){
如果{
虚词_计数(){
$content=get\u post\u字段('post\u content',$post->ID);
$word_count=str_word_count(带标签($content));
返回$word\u count;发布内容;
if(有_短代码($content,'gallery','video')){
//内容有[gallery]和[video]短代码,因此此检查返回true。
}
返回“&hellip;”;
}
否则{
函数主题\u摘录\u长度($length){
返回45;
}
添加过滤器(“摘录长度”、“主题摘录长度”);
}
添加过滤器(“摘录更多”、“个人摘录更多”);

你需要查看你的主题模板文件。如果你想编辑index.php的摘录,那么你可以在该页面上做一个If/else。好的,谢谢你的提示。If/else看起来怎么样?谢谢你的回答。不幸的是,我自己无法编写代码。我的知识还不够基础。我只想增强Wordpress mo重新链接,检查帖子中单词少于45时是否包含图片。