admin管理员组文章数量:1130349
I am trying to create a secondary/alternate version of the_content with some changes. This is the code I found to start with:-
function new_content($content) {
$content = str_replace('<img','<img class="newImgclass"', $content);
$content = str_replace('<p>','<p class="newPclass">', $content);
return $content;
}
add_filter('the_content','new_content');
I want a version of new_content that doesn't affect the original the_content that is when I echo new_content only, all the p have a class="newPclass". Right now, the changes are being applied to the_content.
I am trying to create a secondary/alternate version of the_content with some changes. This is the code I found to start with:-
function new_content($content) {
$content = str_replace('<img','<img class="newImgclass"', $content);
$content = str_replace('<p>','<p class="newPclass">', $content);
return $content;
}
add_filter('the_content','new_content');
I want a version of new_content that doesn't affect the original the_content that is when I echo new_content only, all the p have a class="newPclass". Right now, the changes are being applied to the_content.
1 Answer
Reset to default 1You're overcomplicating things. If you want the_content() to behave as it usually does, then don't change it via filter or similar.
Just create your new custom function, eg like so (could be placed in your functions.php or if you're in a plugin somewhere there)
function replaced_content() {
$content = get_the_content();
// $content = str_replace ...
print $content;
}
Then you can use it just like any other function
<div class="main-content"><?php the_content(); ?></div>
<div class="another-content"><?php replaced_content(); ?></div>
I am trying to create a secondary/alternate version of the_content with some changes. This is the code I found to start with:-
function new_content($content) {
$content = str_replace('<img','<img class="newImgclass"', $content);
$content = str_replace('<p>','<p class="newPclass">', $content);
return $content;
}
add_filter('the_content','new_content');
I want a version of new_content that doesn't affect the original the_content that is when I echo new_content only, all the p have a class="newPclass". Right now, the changes are being applied to the_content.
I am trying to create a secondary/alternate version of the_content with some changes. This is the code I found to start with:-
function new_content($content) {
$content = str_replace('<img','<img class="newImgclass"', $content);
$content = str_replace('<p>','<p class="newPclass">', $content);
return $content;
}
add_filter('the_content','new_content');
I want a version of new_content that doesn't affect the original the_content that is when I echo new_content only, all the p have a class="newPclass". Right now, the changes are being applied to the_content.
-
1
So basically you want to be able to use
the_content();to output the content and another functionnewcontent()to output the changed content? – kero Commented Oct 22, 2018 at 15:03 - Exactly. :) That is what I want. Is that possible? – user1928108 Commented Oct 22, 2018 at 15:03
1 Answer
Reset to default 1You're overcomplicating things. If you want the_content() to behave as it usually does, then don't change it via filter or similar.
Just create your new custom function, eg like so (could be placed in your functions.php or if you're in a plugin somewhere there)
function replaced_content() {
$content = get_the_content();
// $content = str_replace ...
print $content;
}
Then you can use it just like any other function
<div class="main-content"><?php the_content(); ?></div>
<div class="another-content"><?php replaced_content(); ?></div>
本文标签: phpHow do I create a secondary version of thecontent
版权声明:本文标题:php - How do I create a secondary version of the_content 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749241579a2338182.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


the_content();to output the content and another functionnewcontent()to output the changed content? – kero Commented Oct 22, 2018 at 15:03