Support » Fixing WordPress » Force a function to produce a custom HTML code

  • Resolved WHiSPER

    (@apc_whisper)


    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>?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Try it with get_comment_text() [untested]

    <?php
    echo '<p id="comment-text">' . get_comment_text() . '</p>';
    ?>

    Michael

    (@alchymyth)

    Forum Moderator

    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.

    Worked! Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Force a function to produce a custom HTML code’ is closed to new replies.