0

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.

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 Oct 22 '18 at 15:03
  • Exactly. :) That is what I want. Is that possible? – user1928108 Oct 22 '18 at 15:03
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>
2
  • Thanks a lot, this is exactly what I needed. And, yes, I have been overcomplicating. I am sorry, but thank you. – user1928108 Oct 22 '18 at 15:12
  • You are welcome and no worries, that's why talking about this with other people from the field can help. If you feel that this fully answers your question, feel free to mark it as accepted (green tick left of my answer). If not, you can provide more detail in the original question and I'll try to address it – kero Oct 22 '18 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.