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>
the_content();
to output the content and another functionnewcontent()
to output the changed content? – kero Commented Oct 22, 2018 at 15:03