admin管理员组文章数量:1332377
I have some posts with video tag that contains autoplay (they should autoplay when you access the post page).
I am using the the_content
to show these videos on index page.
The problem is that when you access the home page, all the videos start playing.
I have used the following function to remove it:
function replace_ap($text){
$replace = array(
'autoplay=""' => ''
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content', 'replace_ap');
but now the "autoplay" attribute is removed from the post pages also.
How can I remove it only from the index ?
My thought was to create a function that gets the content and add the filter to this function, then call it from index.php instead of "the_content", but I don't know how or if it's possible
Thank you!
I have some posts with video tag that contains autoplay (they should autoplay when you access the post page).
I am using the the_content
to show these videos on index page.
The problem is that when you access the home page, all the videos start playing.
I have used the following function to remove it:
function replace_ap($text){
$replace = array(
'autoplay=""' => ''
);
$text = str_replace(array_keys($replace), $replace, $text);
return $text;
}
add_filter('the_content', 'replace_ap');
but now the "autoplay" attribute is removed from the post pages also.
How can I remove it only from the index ?
My thought was to create a function that gets the content and add the filter to this function, then call it from index.php instead of "the_content", but I don't know how or if it's possible
Thank you!
Share Improve this question asked Jun 30, 2020 at 12:07 CarjeCarje 133 bronze badges 2- i guess you could put conditional tags inside your filter as well: is_home(), is_front_page(), is_single()..? – honk31 Commented Jun 30, 2020 at 13:05
- 1 @honk31 Yes, indeed. Thank you! – Carje Commented Jun 30, 2020 at 13:54
1 Answer
Reset to default 1Few possibilities here:
In index.php you find
the_content()
and replace it with:$content = get_the_content( $more_link_text, $strip_teaser ); $content = apply_filters( 'the_content', $content ); $content = str_replace( ']]>', ']]>', $content ); $replace = array( 'autoplay=""' => '' ); $text = str_replace(array_keys($replace), $replace, $text); echo $text;
Did you try putting that bit of code in index.php? Maybe you have it in functions.php so it will always run but putting it index.php might cause it to only apply there
As @honk31 says you could test for which page you're on inside the function. This might be good if you want this behaviour to apply on other pages later. E.g.
function replace_ap($text){ if (is_home()) { $replace = array( 'autoplay=""' => '' ); $text = str_replace(array_keys($replace), $replace, $text); return $text; } else { return $text; } } add_filter('the_content', 'replace_ap');
本文标签: functionsReplace word in quotthecontentquot only for indexphp
版权声明:本文标题:functions - Replace word in "the_content" only for index.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742306367a2449996.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论