如何修改要在帖子中显示的评论?

时间:2011-07-04 作者:Newbie

我想知道如何修改wordpress在页面中显示评论的方式。手头的要求是在注释列表中显示一定数量的注释,当用户滚动到注释列表的底部(不在页面上)时,注释列表中将附加一组新的注释(如果要显示更多注释)。

我有以下代码调用要显示的注释:

<?php
require_once("../../../wp-config.php");

$page = $_GET[\'cpage\'];
if(isset($page))
{
    echo loadComments($page);                          
}

function loadComments($page=1)
{
    global $wpdb;   

    $number=5;    
    $result=\'\';

    // get comments from WordPress database 
    $numRows = $wpdb->get_var("SELECT COUNT(*)
                            FROM $wpdb->comments 
                            WHERE comment_approved = \'1\' 
                                AND NOT (comment_type = \'pingback\' OR comment_type = \'trackback\')");            

    if ($numRows > $number)
    {
            $nav=1;
            $pages=ceil($numRows/$number);
    }
    else {
        $nav=0;
        $pages=0;
    }

    $getnumber=$number*$page;

    // get comments from WordPress database 
    $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments 
                                    WHERE comment_approved = \'1\' 
                                        AND NOT (comment_type = \'pingback\' OR comment_type = \'trackback\')
                                    ORDER BY comment_date_gmt ASC 
                                    LIMIT $getnumber");     

    $comments=array_slice($comments, $getnumber-$number, $number);          
    if ( $comments )
    {
        $count=1;

        // display comments one by one
        foreach ($comments as $comment)
        {  
            if ($page <= $pages)
            {
                $result.= \'<li id="comment-\'.$comment->comment_ID.\'" class="leftcolumn"><div class="commenttext"><div style="padding:20px 0 0 10px;"><img src="/wp-content/themes/twentyten/images/headers/ImgQuotationMarkOpen.gif" alt="" title="" class="openarrow" />\'.
                        \'</div><div style="padding:0 10px 0 40px;"><p>\'.$comment->comment_content.\'</p></div>\'.
                        \'<div style="padding:0 10px 0 0; text-align:right;"><img src="/wp-content/themes/twentyten/images/headers/ImgQuotationMarkClose.gif" alt="" title="" style="border:0;" class="closearrow"/></div>\'.
                        \'</div></li>\'.\'<li class="middlecolumn"><img src="/wp-content/themes/twentyten/images/headers/ImgBubbleTopRight.gif" class="pointerarrow" /></li>\'.
                        \'<li class="rightcolumn" id=\'.$comment->comment_ID.\'">\'.\'<div style="padding:25px 0 0 10px; line-height:10px;" class="commenttextright">\'.
                        \'<span class="author" style="text-decoration:none;">\'.$comment->comment_author.\'</span><div style="height:5px;"></div>\'.
                        \'<span style="font-family:Arial; font-size:12px;">\'.mysql2date(\'j-n-Y\',$comment->comment_date).\'</span>\'.
                        \'</div></li><div style="clear:both;"></div><div style="height:10px;"></div>\'.
                        \'<div style="height:1px; border-bottom:1px dashed  #3e3e3e;"></div><div style="height:10px;"></div>\';           

                $count++;           
           }
           else
           {
            $result = \'\';
           }
        }
    }   
    return $result;
}

?>
这是一个ajax调用,用于附加一组新的注释:

function updatestatus(){
    //Show number of loaded items
    var totalItems=$(\'.commentlist li div.commenttext\').length;
    $(\'#status\').text(\'Loaded \'+totalItems+\' Items\');
}

function scrollalert(){
    var scrolltop=$(\'.commentlist\').attr(\'scrollTop\');
    var scrollheight=$(\'.commentlist\').attr(\'scrollHeight\'); //825
    var windowheight=$(\'.commentlist\').attr(\'clientHeight\'); //600
    var scrolloffset=20;
    //alert(scrolltop);
    if(scrolltop >= (scrollheight - (windowheight + scrolloffset)))
    {
        //fetch new items
        if (count > 0 )
        {
            count++;
            $(\'#status\').text(\'Loading more items...\');
            $.get(\'/wp-content/themes/twentyten/commentloader.php?cpage=\'+count, \'\', function(newitems){           
                if (newitems != \'\')
                {
                    $(\'.commentlist\').append(newitems);
                    updatestatus();                    
                }   
                else
                {   
                    // prevent from appending content
                    count = -1;
                }
            });
       }
       else
       {
            $(\'#status\').text(\'No more items to load...\');
       }
    }

  setTimeout(\'scrollalert();\', 1500);
}
我尝试在themes文件夹中添加这两个文件,并添加对标题的引用。php for js文件。这是可行的,但我必须为注释设置分页并隐藏导航。如果你能提供一个更好的方法来实现(可能是创建一个插件?)我将不胜感激。我是WordPress的新手,如果你能指引我走向正确的方向,我将非常高兴。

谢谢RNorbe

1 个回复
SO网友:Rarst

WordPress有自己的且非常具体的Ajax实现,为此加载core是错误的。

从阅读开始AJAX in Plugins 在法典中。

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register