切换按钮以显示和隐藏评论和WordPress评论表单

时间:2014-02-26 18:18:37

标签: javascript jquery wordpress

我正在使用WordPress Liveblognot-yet merged pull-request,因此将直接在GitHub回购中引用代码,以便更好地解释我的问题。有关插件代码的实际示例,请参阅here

单击Reply button时会显示WordPress评论表单。在单击按钮之前,表单将隐藏在视图之外。

点击第二个回复按钮> Reply

时会显示评论

我的目标是用一个名为Toggle的按钮替换这两个按钮。点击后,我想要显示WordPress评论表格和评论。

到目前为止,我已经完成了以下工作......

我的切换按钮:

<a class="toggle" href="#">Toggle</a>

我的脚本(从here修改):

jQuery('a.toggle').click(function () {

    var openImgUrl = 'open.png',
        closeImgUrl = 'close.png';

    var $newsItem = jQuery(this).closest('.news-text'),
        $newsContent = $newsItem.find('.news-content'),
        isContentVisible = ($newsContent.is(':visible'));

    // slide up all shown news-items - but its expected that only one is visible at a time
    jQuery('.news-text').find('.news-content').slideUp(function () { 
        // on animation callback change the img
        jQuery('.news-text').find('.toggle > img').attr('src', openImgUrl);
    });

    if (!isContentVisible) { // if the new-item was hidden when clicked, then show it!
        $newsContent.slideDown(function () {
            // on animation callback change the img
            $newsItem.find('.toggle > img').attr('src', closeImgUrl);
        });
    }

    return false; // stop postback
});

使用我的脚本和切换按钮,单击切换按钮时,每个条目下都会显示注释。再次单击切换按钮时,将隐藏注释。但是,这不会考虑WordPress评论表单。

单击切换按钮时,如何让WordPress评论表单与评论一起显示?

Liveblog词汇表:

条目:条目是顶级评论,就像在WordPress中制作的标准“评论”一样。

评论:可以对条目进行评论。

1 个答案:

答案 0 :(得分:0)

我发现comment_reply_link是答案!

http://codex.wordpress.org/Function_Reference/comment_reply_link

使用此功能输出回复链接使我能够做我需要的事情。我只需要在脚本中更改选择器,使用注释回复按钮作为切换,并删除我设置的原始切换按钮。例如:

jQuery('a.comment-reply-link').click(function () {