Support » Fixing WordPress » adding default text on the_content

  • Hi, is there a way to add a default text, sentence inside a <?php the_content(‘Continue Reading’); ?>?

    I would like to have a default message right before the article starts. I can do it by modifying the single.php but by doing so, the message is not included in the_content…

Viewing 6 replies - 1 through 6 (of 6 total)
  • If you are adding to every post you can hook into the the_content filter. Add this to your theme functions.php.

    function add_before_content($content) {
     return 'Default Message'.$content;
    }
    add_filter('the_content', add_before_content);

    Thanks apljdi, however it may not be applicable since I am using 2 single.php (and singele-cat2.php) templates with 2 different “default message” on it.

    Is there a way to add it in single.php inside the_content?

    the_content is really just formatting. The actual post content is (inside the loop) $post->post_content. Try echo $post->post_content and see what I mean. You should be able to do something like $post->post_content = 'Default Message'.$post->post_content; before the call to the_content.

    apljdi, got it to work but not the way I want. Can it be done this way? On category1, this is “default message”, on Category2 “this is another default message” and so on? Thanks again.

    I’ve read through this post and hundreds of others looking for some quick help with this type of topic. In all this procrastination I finally broke down and wrote the following code which allows the user to add text/code/etc before the_content or even after the_content. Use this code on specific pages or with additional php to specify the page you’re on such as Home, Single, etc.

    I am using this code to display a few divs prior to the_content being printed. This is because in some instances I will not be writing any content and I didn’t want the additional divs to mess with the surrounding code by adding margins and padding and headlines where they make no sense without the_content.

    if($content = $post->post_content ) {
    echo "<div class='headline'>";
    echo "<h6>My Headline</h6>";
    the_content();
    echo "</div>";
    } else {
    echo "There is no code to display";
    }
Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘adding default text on the_content’ is closed to new replies.