WordPress.org

Forums

[resolved] Force a function to produce a custom HTML code (4 posts)

  1. WHiSPER
    Member
    Posted 2 years ago #

    Hi,

    I use comment_text() function in a while loop to list comments in my theme. By default this function produce a <p> tag with the comment text in it.

    Now, I want to ask is there any way to add an id to the HTML element <p>?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Try it with get_comment_text() [untested]

    <?php
    echo '<p id="comment-text">' . get_comment_text() . '</p>';
    ?>
  3. unless that css id is dependant on the individul comment, you should rather use a css class;

    because comment_text() uses a filter for the output:

    echo apply_filters( 'comment_text', get_comment_text( $comment_ID ), $comment );

    you can try adding a filter function to functions.php, to be able to influence the html paragraph tag;

    example to add a css class to the first paragraph tag in the comment text(untested):

    add_filter('comment_text','adapt_html_comment_text',30);
    function adapt_html_comment_text($text) {
    $text = preg_replace( '/\<p/', '<p class="comment-text"', $text, 1 );
    return $text;
    }

    details will depend on what exactly you want to add to the p tag.

  4. WHiSPER
    Member
    Posted 2 years ago #

    Worked! Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic