Skip to content

Internal Shortcode | [Post_Comments]

EkoJr edited this page Feb 24, 2017 · 1 revision

Description

Adds a pre-formatted HTML string to display comments in a given post.

Usage

[post_comments before="" after=""]

Params

before

(string) (optional) Adds a string before the comment(s). Useful for adding headers/titles, opening HTML tags, etc. Default: ""

after

(string) (optional) Adds a string before the comment(s). Useful for adding footers, closing HTML tags, etc. Default: ""

Return

(string) Amount of comments.

Examples/Sample

Example 1

[post_comments]

Returns

<p>NAME - USER@EMAIL.COM - Month xx, xxxx @ xx:xx am<br>Comment goes here.</p>

Source

File: advanced-post-list/includes/class/apl_shortcodes.php

/**
 * Post Comment Shortcode. 
 * 
 * Desc: Adds an HTML string to display comments in a set format.
 * 
 * 1. Get Kalin's PDF comments. Support old method. 
 * 2. Get 'approved' comments from Post ID. 
 * 3. Add to return the Before Attribute. 
 * 4. For each Post_Comments, add formatted string to return, w/ author link 
 *    if available.
 * 5. Add to return the After Attribute. 
 * 6. Return string.
 * 
 * @since 0.1.0 
 * @version 0.4.0 - Changed to Class function, and uses WP's built-in
 *                  functions for setting default attributes & do_shortcode().
 * 
 * @link https://codex.wordpress.org/Function_Reference/get_comments
 * 
 * @param array $atts {
 *      
 *      Shortcode Attributes. 
 *      
 *      @type string $before    Adds a string before the list. 
 *      @type string $after     Adds a string after the list.
 * }
 * @return string Post Excerpt OR formatted Post_Content.
 */
public function post_comments($atts)
{
    //INIT
    $atts_value = shortcode_atts( array(
        'before' => '',
        'after' => ''
    ), $atts, 'post_comments');
    $return_str = '';
    
    //STEP 1
    if (defined("KALINS_PDF_COMMENT_CALLBACK"))
    {
        return call_user_func(KALINS_PDF_COMMENT_CALLBACK);
    }
    
    
    //STEP 2
    $args = array(
        'status' => 'approve',
        'post_id' => $this->_post->ID
    );
    $post_comments = get_comments($args, $this->_post->ID);
    
    //STEP 3
    $return_str .= $atts_value['before'];
    
    //STEP 4
    foreach ($post_comments as $comment)
    {
        if ($comment->comment_author_url == "")
        {
            $comment_author = $comment->comment_author;
        }
        else
        {
            $comment_author = '<a href="' . $comment->comment_author_url . '" >' . $comment->comment_author . "</a>";
        }
        
        $return_str .= '<p>' . $comment_author . 
                       " - " . $comment->comment_author_email . 
                       " - " . get_comment_date(null, $comment->comment_ID) . 
                       " @ " . get_comment_date(get_option('time_format'), $comment->comment_ID) . 
                       "<br />" . $comment->comment_content . "</p>";
    }
    
    //STEP 5
    $return_str .= $atts_value['after'];
    
    //STEP 6 
    return $return_str;
}

Additional Resources

Clone this wiki locally