简体   繁体   English

在 WP 注释之间插入 HTML 代码

[英]Insert HTML code between WP comments

I want to insert HTML code after/below every 5th comment in WP (if there are > 5 comments).我想在 WP 中每 5 条评论之后/下方插入 HTML 代码(如果有 > 5 条评论)。

I'm not good at coding, and I only found 1 similar topic, that wasn't answered.我不擅长编码,我只找到了 1 个类似的主题,没有回答。

Question is - how do in insert an Ad/Code after every 5th comment?问题是 - 如何在每 5 条评论后插入一个广告/代码?

I'm not good with PHP (very basic experience) ... please provide complere code, if possible - THANKS!我不擅长 PHP(非常基本的经验)......如果可能,请提供完整的代码 - 谢谢!

Here is my function (from functions.php) that displays comments:这是我的显示注释的函数(来自functions.php):

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>代码示例: <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未经测试,但我建议您可以将评论迭代计数设置为全局范围,然后每第三个模数除法,回显您的 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']++;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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