10

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 Answers 2

10

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

4
  • Much power in that period, thanks! Another question: I am using a plugin to display social buttons for Like, +1 etc and they are appearing above the new-default-content — is there a way to get the buttons to appear below? I assumed this filter would "chunk" the_content & new_default_content as one complete section.
    – George C
    Sep 13, 2011 at 15:45
  • 1
    Well, you're appending the new content, so it's going to appear afterward. You might try hooking the filter in at a higher priority to get it to process before the plugin does, because the plugin is also using the same filter. Sep 13, 2011 at 17:27
  • Ah, I will look into the priorities thing — thanks again!
    – George C
    Sep 14, 2011 at 5:46
  • FYI I played around with the priority and that did the trick! Thanks again :)
    – George C
    Sep 14, 2011 at 17:56
-1

In Simple way `

               if ($post->post_type == 'custom-post-type') {
                   $new_content= 'Test text here';
                                }
                       return $content.$new_content;
1
  • Only code is not considered a good quality answer. You should explain what the code does and how it answers the question; in this way readers who are newbies in coding will understand your answer. Can you explain your code?
    – cybmeta
    Nov 18, 2015 at 12:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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