显示用户评论列表+帖子标题+日期

时间:2021-01-21 作者:cloyd concepcion

我正在尝试在特定页面中显示当前用户的所有注释,并且我正在使用此代码(使用快捷码):https://blog.ashfame.com/2011/01/show-recent-comments-particular-user-wordpress/

并对其进行了修改:

<?php
/*
Plugin Name: Show Recent Comments by a particular user
Plugin URI: http://blog.ashfame.com/?p=876
Description: Provides a shortcode which you can use to show recent comments by a particular user
Author: Ashfame
Author URI: http://blog.ashfame.com/
License: GPL
Usage: 
*/

add_shortcode ( \'show_recent_comments\', \'show_recent_comments_handler\' );

function show_recent_comments_handler( $atts, $content = null )
{
    extract( shortcode_atts( array( 
        "count" => 10,
        "pretty_permalink" => 0
        ), $atts ));

    $output = \'\'; // this holds the output
    
    if ( is_user_logged_in() )
    {
        global $current_user;
        get_currentuserinfo();

        $args = array(
            \'user_id\' => $current_user->ID,
            \'number\' => $count, // how many comments to retrieve
            \'status\' => \'approve\'
            );

        $comments = get_comments( $args );
        if ( $comments )
        {
            $output.= "<ul>\\n";
            foreach ( $comments as $c )
            {
            $output.= \'<li>\';
            if ( $pretty_permalink ) // uses a lot more queries (not recommended)
                $output.= \'<a href="\'.get_comment_link( $c->comment_ID ).\'">\';
            else
                $output.= \'<a href="\'.get_settings(\'siteurl\').\'/?p=\'.$c->comment_post_ID.\'#comment-\'.$c->comment_ID.\'">\';           
            $output.= $c->comment_content;
            $output.= \', Post Name: \'.get_the_title($c->comment_post_ID);
            $output.= \'</a>, Posted on: \'. mysql2date(\'m/d/Y\', $c->comment_date, $translate);
            $output.= "</li>\\n";
            }
            $output.= \'</ul>\';
        }
    }
    else
    {
        $output.= "<h2>You should be logged in to see your comments. Make sense?</h2>";
        $output.= \'<h2><a href="\'.get_settings(\'siteurl\').\'/wp-login.php?redirect_to=\'.get_permalink().\'">Login Now &rarr;</a></h2>\';
    }
    return $output;
}
?>

可以显示如下注释:

帖子标题(用户留下评论的帖子标题)

评论摘录(这是我对……的评论)

  • 评论日期(用户发表评论的帖子标题)

    评论摘录(这是我对……的评论)

  • 评论日期(用户发表评论的帖子标题)

    评论摘录(这是我对……的评论)

  • 评论日期
      • 因为使用我当前的代码看起来不太好,请参见此处的屏幕截图:https://prnt.sc/xd8qmc

        是否也可以将评论分页?

    1 个回复
    SO网友:HK89

    根据需要添加以下代码段:

    add_shortcode ( \'show_recent_comments\', \'show_recent_comments_handler\' );
    
    function show_recent_comments_handler( $atts, $content = null )
    {
        extract( shortcode_atts( array( 
            "count" => 10,
            "pretty_permalink" => 0
            ), $atts ));
    
        $output = \'\'; // this holds the output
        
        if ( is_user_logged_in() )
        {
            global $current_user;
            get_currentuserinfo();
    
            $args = array(
                \'user_id\' => $current_user->ID,
                \'number\' => $count, // how many comments to retrieve
                \'status\' => \'approve\'
                );
    
            $comments = get_comments( $args );
            if ( $comments )
            {
                $output.= "<ul>\\n";
                foreach ( $comments as $c )
                {
                $output.= \'<li>\';
    
                $output.= \'Post Name: \'.get_the_title($c->comment_post_ID);
                $output.= "<ul>";
    
                if ( $pretty_permalink ) // uses a lot more queries (not recommended)
                    $output.= \'<a href="\'.get_comment_link( $c->comment_ID ).\'">\';
                else
                    $output.= \'<a href="\'.get_settings(\'siteurl\').\'/?p=\'.$c->comment_post_ID.\'#comment-\'.$c->comment_ID.\'">\';        
                $output.=\'<li>\'. $c->comment_content .\'</li>\';
                $output.= \'</a><li> Posted on: \'. mysql2date(\'m/d/Y\', $c->comment_date, $translate).\'</li>\';
                $output.= \'</ul>\';
                $output.= "</li>\\n";
                }
                $output.= \'</ul>\';
            }
        }
        else
        {
            $output.= "<h2>You should be logged in to see your comments. Make sense?</h2>";
            $output.= \'<h2><a href="\'.get_settings(\'siteurl\').\'/wp-login.php?redirect_to=\'.get_permalink().\'">Login Now &rarr;</a></h2>\';
        }
        return $output;
    }
    

    相关推荐

    Execute shortcodes in PHP

    我在帖子编辑器中添加了一个地理位置快捷码,可以根据用户的位置显示不同的信息,将其放在帖子页面的内容中,效果非常好。如何在主题的PHP文件中执行此短代码[地理位置][地理位置]?我正在使用免费修改Wordpress主题,我想添加一个新的<span>[geolocation][/geolocation]Information text content</span> 但在主题的内部。php文件,我如何才能做到这一点?如果我直接加上<span>[geolocation][/ge