php - How do I create a secondary version of the_content

admin2025-06-07  51

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.

Share Improve this question asked Oct 22, 2018 at 15:01 user1928108user1928108 251 silver badge7 bronze badges 2
  • 1 So basically you want to be able to use the_content(); to output the content and another function newcontent() 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
Add a comment  | 

1 Answer 1

Reset to default 1

You'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>
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749241603a317546.html

最新回复(0)