简体   繁体   中英

Insert HTML code between WP comments

I want to insert HTML code after/below every 5th comment in WP (if there are > 5 comments).

I'm not good at coding, and I only found 1 similar topic, that wasn't answered.

Question is - how do in insert an Ad/Code after every 5th comment?

I'm not good with PHP (very basic experience) ... please provide complere code, if possible - THANKS!

Here is my function (from functions.php) that displays comments:

if ( ! function_exists( 'mts_comments' ) ) {
function mts_comment($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment; ?>
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
        <div id="comment-<?php comment_ID(); ?>" style="position:relative;" itemscope itemtype="http://schema.org/UserComments">
            <div class="comment-author vcard">
                <?php echo get_avatar( $comment->comment_author_email, 70 ); ?>
                <div class="comment-metadata">
                <?php printf('<span class="fn" itemprop="creator" itemscope itemtype="http://schema.org/Person">%s</span>', get_comment_author_link()) ?>
                <time><?php comment_date(get_option( 'date_format' )); ?></time>
                <span class="comment-meta">
                    <?php edit_comment_link(__('(Edit)', 'point'),'  ','') ?>
                </span>
                <span class="reply">
                    <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
                </span>
                </div>
            </div>
            <?php if ($comment->comment_approved == '0') : ?>
                <em><?php _e('Your comment is awaiting moderation.', 'point') ?></em>
                <br />
            <?php endif; ?>
            <div class="commentmetadata" itemprop="commentText">
                <?php comment_text() ?>
            </div>
        </div>
    </li>
<?php }
}

So far, i figured out how to get count of Approved comments. Here is my "code":

$cmPostId = get_the_ID();
$comments_count = wp_count_comments($cmPostId);
$commApproved = $comments_count->approved;

Code examle: <div>HTML HERE</div>

I appreciate any and all help. Thanks in advance!

Untested but I propose you could set a comment iteration count into global scope and then every 3rd modulus division, echo your HTML

if ( ! function_exists( 'mts_comments' ) ) {
$comment_count = 1;
function mts_comment($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment; 
    ?>
    <li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
        <div id="comment-<?php comment_ID(); ?>" style="position:relative;" itemscope itemtype="http://schema.org/UserComments">
            <div class="comment-author vcard">
                <?php echo get_avatar( $comment->comment_author_email, 70 ); ?>
                <div class="comment-metadata">
                <?php printf('<span class="fn" itemprop="creator" itemscope itemtype="http://schema.org/Person">%s</span>', get_comment_author_link()) ?>
                <time><?php comment_date(get_option( 'date_format' )); ?></time>
                <span class="comment-meta">
                    <?php edit_comment_link(__('(Edit)', 'point'),'  ','') ?>
                </span>
                <span class="reply">
                    <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
                </span>
                </div>
            </div>
            <?php if ($comment->comment_approved == '0') : ?>
                <em><?php _e('Your comment is awaiting moderation.', 'point') ?></em>
                <br />
            <?php endif; ?>
            <div class="commentmetadata" itemprop="commentText">
                <?php comment_text() ?>
            </div>
        </div>
        <?php if ($GLOBALS['comment_count'] % 3 == 0): ?>
        <div>HTML HERE</div>
        <?php endif ?>
    </li><?php 
        $GLOBALS['comment_count']++;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM