在WP注释之间插入HTML代码

时间:2017-10-04 22:38:48

标签: php wordpress foreach wordpress-theming

我想在WP中的每第5条评论之后/之下插入HTML代码(如果有> 5条评论)。

我编码不好,我只发现了一个类似的主题,但没有回答。

问题是 - 每隔5条评论后如何插入广告/代码?

我不擅长使用PHP(非常基本的经验)...如果可能的话,请提供完整的代码 - 谢谢!

这是我的函数(来自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 }
}

到目前为止,我想出了如何获得批准的评论。这是我的#34;代码&#34;:

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

代码考试:<div>HTML HERE</div>

我感谢任何帮助。提前谢谢!

1 个答案:

答案 0 :(得分:1)

未经测试但我建议您可以将注释迭代计数设置为全局范围,然后每个3模数除法,回显您的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']++;
    }
}
相关问题