Linking to Page Showing Only Comments Without Parent Post

I’d like to show post comments on their own page without the parent post. I know I can use wp_list_comments() on the single post page and pass a callback function to use my own comment display markup. I plan to do this so I can include a link with each comment that will show that comment on it’s own page.

If this weren’t WordPress I’d use:

Read More
<a href = " www.example.com/individual_comment.php?comment_id = $comment_id">View single comment</a>

…and grab the $comment_id from the query string.

What would that link look like in WordPress? ie: what string would I include to get directly to, let’s say, my_comments.php where I call get_comment($comment_id) and comment_template()?

<a href = "<?php bloginfo('url');?>/what/goes/here?comment_id = $comment_id"<View single comment</a>

Related posts

Leave a Reply

2 comments

  1. You could probably just create a new page in WordPress, and give that page a custom template. Then the url would just be whatever it normally would be to get to that page. The only difference is that the custom template you are using would be setup to accept the comment_id via the querystring, and then just get the details for the specific comment, and in the template code echo out the details of the comment.

    So, if you have a page in wordpress called “Comment Details” that you create, you could access that page via http://www.domain.com/comment-details (assuming you have permalinks enabled). So your link would look like this:

    <a href = "<?php bloginfo('url');?>/comment-details?comment_id=$comment_id">View single comment</a>
    

    The “Comment Details” page would be set up to use a custom template that would contain the code to spit out the details.

  2. There are numerous different ways to accomplish this, some more polished than others and practically all of them with potential for conflict with other plugins, but ignoring all that here’s one way that is pretty close to what you asked for. 🙂

    This solution will support a URL format like the following where %comment_id% is the numeric ID of your comment from the wp_comments table:

    http://example.com/comments/%comment_id%/

    First you’ll need to configure your URL rewriting using the following code. Hopefully it is reasonably self-explanitory but don’t hesitate to ask:

    $wp->add_query_var('comment_id');  // Add the "behind-the-scenes" query variable that WordPress will use
    $wp_rewrite->add_rewrite_tag('%comment_id%', '([0-9]+)','comment_id=');  // Define a rewrite tag to match that assigns to the query var 
    $wp_rewrite->add_permastruct('comment-page', 'comments/%comment_id%');   // Define a URL pattern to match the rewrite tag.
    

    You’ll also either need to call this code in a plugin activation hook to flush the rules, or if it’s your site you can just save permalinks in the admin console’s Settings > Permalinks settings area:

    global $wp_rewrite;
    $wp_rewrite->flush_rules(false);
    

    Next add a parse_query filter hook. This will be called after WordPress has inspected the query. It tests to see if your added comment_id query_var set and if so it tests to see if you are on the desired URL. If yes then it loads the comment array using get_comment() in order to set the 'p' parameter (which should be set to a post ID) to the post that is related to the comment. That way when WordPress runs the query that it is going to run no matter what at least it loads something you’ll need in your comment.php theme template file below and you won’t have to ran another query later when you need it. This code also tells WordPress to ignore sticky posts using the oddly named caller_get_posts option:

    add_filter( 'parse_query', 'my_parse_query' );
    function my_parse_query( $query ) {
        global $wp;
        if (isset($query->query['comment_id']) && substr($wp->request,0,9)=='comments/') { 
            $comment = get_comment($query->query['comment_id']);
            $query->query_vars['p'] =  $comment->comment_post_ID; // Causes the comment's post to be loaded by the query.
            $query->query_vars['caller_get_posts'] = true;  // Keeps sticky posts from invading into the top of our query.
        }
    }
    

    Still next you’ll need to hook the code in /wp-includes/template-loader.php using the template_include filter. This will be called after WordPress has both inspected the query and loaded the post for the comment. Here you’ll first check again for comment_id in the query_var and also for the URL being the one you want. If so we replace the /index.php template page with /comment.php which is a theme template file you will need to create:

    add_filter( 'template_include', 'my_template_include' );
    function my_template_include( $template ) {
        global $wp,$wp_query;
        if (isset($wp_query->query['comment_id']) && substr($wp->request,0,9)=='comments/') {
            $template = str_replace('/index.php','/comment.php',$template);
        }
        return $template;
    }
    

    Lastly now you need to create your theme template file which I’ve chosen to call /comment.php. Since it’s your theme you’ll want to make it look like you want but here is an example to get you started:

    <?php 
    /*
     *  File: /wp-content/themes/my-theme/comment.php
     */ 
    global $wp_query,$post;
    $comment_id = $wp_query->query['comment_id'];
    $comment = get_comment($comment_id);
    $permalink = get_permalink($post->ID);
    get_header();
    ?>
    <div id="container">
        <div id="comment-<?php echo $comment_id; ?>" class="comment">
            <p>Comment by: <span class="comment-author">
                <a href="<?php echo $comment->comment_author_url; ?>"><?php echo $comment->comment_author; ?></a></span>
                on <span class="comment-date"><?php echo date("D M jS Y", strtotime($comment->comment_date)); ?></span>
              at <span class="comment-time"><?php echo date("h:ia", strtotime($comment->comment_date)); ?></span>
            </p>
            <p>About: <a href="<?php echo $permalink; ?>"><?php echo $post->post_title; ?></a></p>
            <blockquote><?php echo $comment->comment_content; ?></blockquote>
        </div>
    </div>
    <?php 
    get_sidebar();
    get_footer();
    

    Any questions? Just ask.

    P.S. All of the code I describing above can either go in your theme’s functions.php file and/or in a plugin of your own. A caveat is for the URL rewrite flushing rules that should go in a plugin activation hook if you are going to include it instead us just flushing them manually in the permalinks section of the admin console. I didn’t show how to register an activation hook do but if you want to learn more you can read about it here.