I need to display link after each comment, when you click that link, a new page displays that single coment on a new page.
Is that possible?
I need to display link after each comment, when you click that link, a new page displays that single coment on a new page.
Is that possible?
I answered your exact question over on WordPress Answers (also a StackExchange site) just yesterday. You can find that answer here. It involved the following four steps:
query_var
,rewrite_tag
and apermastruct
,parse_query
filter hook to set thequery_vars
's post to be the comment's post and to disable sticky posts for the query,template_include
filter hook to filter the template file name to load a template specific template file for a single comment, and lastly/wp-content/themes/%your-theme%/comment.php
.Again, you can find the answer over here.
Hope this helps.
-Mike
UPDATE:
Below is the full content that I had also posted on WordPress Answers:
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.
Hope this helps.
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.(New edited version after OP's comments)
There are many ways to do this. In theory this is the simplest, but maybe not 'most appropriate according to WordPress' way. Take this as a starting point. I haven't tested it, so you may encounter an error or two that should be solvable with some minor tweaks. Let me know if you get stumped and I'll do my best. So conceptually...
You should copy the page.php template file and rename it to 'comments_page.php' (or whatever you like). Open this file in your code editor and find where the following appears: (or create it if it does not exist)
and change it to
Now open your WordPress admin area and create a new page. Call it whatever you want but don't add any content. In the right hand column, select the template that the page uses from the "Page Template" drop down menu. Select 'comments_page' (or whatever you listed as the template name). This tells WordPress to use your file to show this specific page instead of the default page template. Save the page and note the page_id that WordPress generates.
Now, find your theme's comments template, usually 'comments.php'. Find the function
wp_list_comments();
. We are going to add the name of a custom function that will control the display of your comments as an argument to this function. For an example, go to the twenty-ten theme's files, open comments.php and you'll see what that looks like:Open the twenty-ten theme's functions.php and find
Copy that entire function and paste it into your theme's functions file. Change the name to' my_comment()', and add that to the wp_list_comments function call like this:
In your newly-created 'my_comment()' function in your functions.php file, add a link where you want to the separate page of comments (comments_page.php) using get_page_link() and a query var named 'commentID' and the comments ID.
Now to inappropriately add php logic to a template file. Once you understand how this works, you can create a function in your functions.php file and then call it in the theme file...
On comments_page.php ,use $_GET['commentID'] to retrieve the comment's id value from the url, and pass it to get_comment($commentID) to retrieve the single comment and display it on a single page.
Now you have all the single comments information in the $comment variable as an object.
You can decide how to display the comment, but to start, I recommend copying the contents of your theme's comments template to keep things consistent. It will show exactly the same thing the post page shows, but it sounds like this page is intended more for the permalink to a single comment that you link to from somewhere else.
Hope this helps. Let me know if you run into a snag.
Note: this answer provides info given to me from Todd Perkins over at wordpress.stackexchange.com
This is my functions.php
This is my comments_page.php
This is my comments.php