Wpdb获取头像和作者URL

时间:2014-07-30 作者:andy

运行SQL查询,通过作者在帖子中获得的投票来获取作者。

@邦格在我的第一篇文章中帮了我,并帮助我把这篇文章做得很好,但我提出了这个新问题,因为这是一个单独的问题。

<?php


                    global $wpdb;

                    $date2 = date(\'Y-m-d\', strtotime(\'-60 days\'));

                     $weekly = $wpdb->get_results ( "
                   SELECT u.display_name as name,
                    ( SELECT
                      CONCAT_WS(\'.\',
                        SUM(CASE WHEN p.post_date > \'$date2\' THEN pm.meta_value ELSE 0 END),
                        SUM(pm.meta_value))
                      FROM wp_posts p, wp_postmeta pm
                      WHERE p.post_author = u.ID
                      AND p.post_status LIKE \'publish\'
                      AND pm.meta_key = \'epicredvote\'
                      AND p.ID = pm.post_id ) as votes
                      FROM wp_users u
                      ORDER BY votes+0 DESC LIMIT 0,5
                " );




                    foreach ($weekly as $result) {
                        $name = $result->name;
                        list( $weekly_vote, $total_vote ) = explode( \'.\', $result->votes ? $result->votes : \'0.0\' );
                        // do stuff with $name, $weekly_vote, $total_vote ...eg
                        echo $name, \'<br>\',  \' Weekly Karma: \', $weekly_vote, \'<br>\',\' Total Karma: \', $total_vote, \'<br><br><br>\';
                    }

                ?>
在做了一些环顾之后,我遇到了一些与此集成时遇到困难的示例代码。

我想有一个指向作者URL的名称链接,并显示作者头像。

类似->

<a href="\' . get_author_posts_url( $author->ID ) . \'">\' . get_the_author_meta( \'user_nicename\' , $author->ID ) . \' 
            \' . get_avatar( $author->ID , 40 ) . \'
            </a> \' Weekly Karma: \', $weekly_vote, \'<br>\',\' Total Karma: \', $total_vote, \'<br><br><br>\';

1 个回复
最合适的回答,由SO网友:bonger 整理而成

您只需要在主选择中返回用户ID,并将其用作作者IDecho 使用逗号(独立参数)而不是点(一个串联参数)。

$weekly = $wpdb->get_results ( "
                       SELECT u.ID,
                        ( SELECT
                          CONCAT_WS(\'.\',
                            SUM(CASE WHEN p.post_date > \'$date2\' THEN pm.meta_value ELSE 0 END),
                            SUM(pm.meta_value))
                          FROM wp_posts p, wp_postmeta pm
                          WHERE p.post_author = u.ID
                          AND p.post_status LIKE \'publish\'
                          AND pm.meta_key = \'epicredvote\'
                          AND p.ID = pm.post_id ) as votes
                          FROM wp_users u
                          ORDER BY votes+0 DESC LIMIT 0,5
                    " );
foreach ($weekly as $author) {
    list( $weekly_vote, $total_vote ) = explode( \'.\', $author->votes ? $author->votes : \'0.0\' );
    echo \'<a href="\' . get_author_posts_url( $author->ID ) . \'">\' . get_the_author_meta( \'user_nicename\' , $author->ID ) . \' \' . get_avatar( $author->ID , 40 ) . \' </a> \'
        . \' Weekly Karma: \' . $weekly_vote . \'<br>\' . \' Total Karma: \' . $total_vote . \'<br><br><br>\';
}

结束

相关推荐

无法使用AJAX结果从SQL获取结果

我正在使用一个AJAX表单向代码提供一些东西,以便在db中查找。单独的表格,而不是WP选项。我从AJAX获得结果,但当我尝试执行sql时,它只返回0。这是功能: function myajax_inputtitleSubmit_func() { // check nonce $nonce = $_POST[\'nextNonce\']; if ( ! wp_verify_nonce( $nonce, \'myajax-next-nonce\' )