【问题标题】:Insert HTML code between WP comments在 WP 注释之间插入 HTML 代码
【发布时间】:2018-03-16 11:00:45
【问题描述】:

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

我不擅长编码,我只找到了 1 个类似的主题,没有得到回答。

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

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

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

到目前为止,我想出了如何获得 Approved cmets 的计数。这是我的“代码”:

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

代码示例:&lt;div&gt;HTML HERE&lt;/div&gt;

我感谢任何和所有的帮助。提前致谢!

【问题讨论】:

    标签: php wordpress foreach wordpress-theming


    【解决方案1】:

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

    【讨论】:

    • np,快乐编码
    猜你喜欢
    • 2014-06-22
    • 2017-03-15
    • 2013-06-19
    • 2011-04-14
    • 1970-01-01
    • 2012-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多