The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

Howto get substring of the_content() ??? (7 posts)

  1. uzbek
    Posted 10 years ago #

    Hi there,

    I've been playing with WP for some time, currently trying to use it for some college website. And what I need is to get substring of the_content().

    I guess it echo's the string within the function, is there a way to get the content returned, rather than echoed out?!

    Read the documentation, searched the support forums, but nothing came up!

    Thanks in advance...

  2. manstraw
    Posted 10 years ago #

    What are you trying to do? Perhaps there is another solution to the one you are considering.

  3. uzbek
    Posted 10 years ago #

    Hi manstraw,

    Thanks for your reply. I was trying to get the contetn of the post to some php variable. Using the_content() echo's it straight away and there is no way of playing with it.

    However, I found a workaround. I queried the DB and got everything I wanted in an array :)

    This article in codex was very helpfull.

    Thanks yet again...

  4. Alex Mills (Viper007Bond)
    Posted 10 years ago #

    Use $post->post_content or a filter.

  5. uzbek
    Posted 9 years ago #

    My appologies for being so late.

    Gosh, I should have spent more time looking for that class...

    Thanks Viper007Bond.

  6. Phil Johnston
    Posted 9 years ago #

    I want to store the output of the_content in a PHP variable as well. However, I don't think I can use post->post_content because I need the code to be HTML - (so it includes the
    tags and <p> tags and stuff). Is there any way to store that code in a variable?

    eg- either have post->post_content output as HTML
    or- store the_content in a variable


  7. Phil Johnston
    Posted 9 years ago #

    ok so I figured it out. For anyone trying to do this in the future....

    Open the file "wp-includes/template-functions-post.php"
    On line 74 or so there is a function called "the_content"
    Copy that whole function and paste it below
    Rename it to something like "the_content_modified"
    Change the line that reads "echo $content;"
    To "return $content;"

    Now you can store it in a variable using:
    $myVariable = htmlspecialchars (the_content_modified('', 0, ''));

    *this example will keep all the HTML tags


Topic Closed

This topic has been closed to new replies.

About this Topic