$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How do I create a secondary version of the_content|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How do I create a secondary version of the_content

matteradmin9PV0评论

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>
Post a comment

comment list (0)

  1. No comments so far