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:
<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>
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:
The “Comment Details” page would be set up to use a custom template that would contain the code to spit out the details.
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 thewp_comments
table: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:
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:
Next add a
parse_query
filter hook. This will be called after WordPress has inspected the query. It tests to see if your addedcomment_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 usingget_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 yourcomment.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 namedcaller_get_posts
option:Still next you’ll need to hook the code in
/wp-includes/template-loader.php
using thetemplate_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 forcomment_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: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: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.