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

  1. WHiSPER
    Posted 2 years ago #


    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
    Posted 2 years ago #

    Try it with get_comment_text() [untested]

    echo '<p id="comment-text">' . get_comment_text() . '</p>';
  3. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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):

    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
    Posted 2 years ago #

    Worked! Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic