• Resolved skywalker826

    (@skywalker826)


    Dear WordPress Guru,

    I would like to modify the output of the_content, as I couldn’t find where to modify it.

    I have:

    <div class="storycontent">
        <?php the_content(__('(more...)')); ?>
    </div>

    and I checked that the html source output from browser is this:

    <div class="storycontent">
    
    <table style="height: 200px;" border="0" cellspacing="1" cellpadding="0" width="600">
    <tbody>
    <tr>
    <td width="200"><strong><a href="../test1/">test1</a></strong></td>
    <td width="200"><strong><a href="../test2/">test2</a></strong></td>
    <td width="200"><strong><a href="../test3/">test3</a></strong></td>
    </tr>
    </tbody>
    </table>
    
    </div>

    So now, I want to modify the table to insert a class=”link” and class=”tdataclass”, like below:
    <table class="link" style="height: 200px;" border="0" cellspacing="1" cellpadding="0" width="600">
    and also:
    <td width="200" class="tdataclass">

    Can you please tell where exactly I can make those changes to the PHP code ?

    Many thanks.

    cheers,
    Sky

Viewing 5 replies - 1 through 5 (of 5 total)
  • To alter the output of the content you need to put something like this in your theme’s function.php.

    function add_before_content($content) {
      return 'Hey! I just put stuff before the content. '.$content;
    }
    add_filter('the_content', add_before_content);

    What version of WP are you using? What theme? And do you have an URL?

    Thread Starter skywalker826

    (@skywalker826)

    Hi apljdi,

    thanks for reply.

    $wp_version = ‘2.8.4’;
    theme is praggio..

    if I use add_filter to add in the content, like you said, it will add something instead of modify/replace something. am i right ?
    Do I code the following way ? the output should be dynamic..

    function add_before_content($content) {
    		$content = "<table class=\"link\">";
    		$content .= "";
    		$content .= "</table>";
           return $content;
    }
    add_filter('the_content', add_before_content);

    But I suppose I should not add something before the content, instead I should amend or replace the output..

    Appreciate your kind advice…

    sky

    if I use add_filter to add in the content,

    Well, if you use the function I supplied it will just add rather trivial content, but the same filter can be used with a different function to modify or replace content. My function was just a very simple example. ‘add_filter’ by itself just adds a function that processes the content. How it processes the content depends on the function. Your code will just produce an empty table over-writing existing content data. You’ll need to use something like preg_replace() to add the classes you want without over-writing the existing content data.

    Thread Starter skywalker826

    (@skywalker826)

    hi apljdi,

    Thank you very much.

    with <?php the_content(__(‘(more…)’)); ?>
    turn out to be a table data:
    <table…>


    </table>

    I’m thinking how actually from where the content data is being populated, is it being retrieved from database level ? or defined somewhere.
    I checked all the files in wp-includes/* , I couldn’t find anything that define that.

    I still puzzle…from where does the
    the_content(__('(more...)'));
    get the content data from specifically?

    cheers,
    Sky

    I’m thinking how actually from where the content data is being populated, is it being retrieved from database level ?

    Yes. The content is stored in the database in wp_posts table in the post_content column. But WordPress does not return post_content as a table by default. Something else is doing that. Unless you’ve created a table in the post by basically typing in the html for a table in the post edit form, its probably your theme that is generating the table using post_content for bits of it.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to modify the output of the_content’ is closed to new replies.