How to appending to the_content using add_filter with custom post type?

I have a custom post type running fine, but some of the text in the page is the same for every post, so I want to add it in using a function.

I have this set up:

function new_default_content($content) {
global $post;
    if ($post->post_type == 'custom-post-type') {
    $content="Test text here";
    }
    return $content;
    }
add_filter('the_content', 'new_default_content');

However when I refresh the page, I only see “Text test here” and not the the post’s content (from the_content).

If I comment out this function, the post content reappears. What am I doing wrong?

2 s
2

You’re completely overwriting the content instead of appending it. You need to do something like $content .= 'Test text here'; instead.

Leave a Comment