admin管理员组文章数量:1429629
I would like to have a separate page for the comments on a post, such that this page: /2011/02/post-name/
just shows the post and then you click a link to view the comments, taking you to a page like this: /2011/02/post-name/comment-page-1/
I will then check the URL to see if "comment-page-x" is in it, and display the page differently if so (remove the post, so people don't have to scroll past that every time, and also style things a bit differently).
This would work, but WordPress redirects comment-page-1
to the post page - I need it to keep comment-page-1
there in the URL.
Is this possible? How can I do it? Thanks!
I would like to have a separate page for the comments on a post, such that this page: /2011/02/post-name/
just shows the post and then you click a link to view the comments, taking you to a page like this: /2011/02/post-name/comment-page-1/
I will then check the URL to see if "comment-page-x" is in it, and display the page differently if so (remove the post, so people don't have to scroll past that every time, and also style things a bit differently).
This would work, but WordPress redirects comment-page-1
to the post page - I need it to keep comment-page-1
there in the URL.
Is this possible? How can I do it? Thanks!
Share Improve this question asked Feb 11, 2011 at 11:42 ShaunShaun 8933 gold badges14 silver badges26 bronze badges 1- 1 upvotet the Q. As Mike said: good one :) – kaiser Commented Feb 11, 2011 at 17:07
1 Answer
Reset to default 9Great question! WordPress assigns your comment page number to the query var 'cpage'
which gets set when your URL has /comment-page-1/
at the end. So your culprit is in the redirect_canonical()
function, line 192 of /wp-includes/canoncial.php
.
if ( get_query_var('paged') || is_feed() || get_query_var('cpage') ) {
Since the redirect_canonical()
function gets set as an action what we can do is to insert our own function to be called instead, have our function set the 'cpage'
query var to false
, call redirect_canonical()
, and then set 'cpage'
back to what it was; that will keep WordPress from redirecting on you.
To insert your own function you need to call the two hook 'init'
and 'template_redirect'
like so being sure to set the 'init'
hook to be called after the do_action()
inside WordPress core that adds redirect_canonical()
:
add_action('init','yoursite_init',11); //11=lower priority
function yoursite_init() {
remove_action('template_redirect','redirect_canonical');
add_action('template_redirect','yoursite_redirect_canonical');
}
add_action('template_redirect','yoursite_redirect_canonical');
function yoursite_redirect_canonical($requested_url=null, $do_redirect=true) {
$cpage = get_query_var('cpage');
set_query_var('cpage',false);
redirect_canonical($requested_url, $do_redirect);
set_query_var('cpage',$cpage);
}
Then of course you need to do something with your 'cpage'
. You can either check for the value returned by get_query_var('cpage')
or you can add another hook to allow you to create a comment-specific template which is what I did. It will add look for a theme template file with the same as it would normally load but with [comments].php
at the end of the name instead of .php
, i.e. single[comments].php
. Note that I set priority for this filter to be 11; you may need to set to an even larger number if a plugin you use adds itself after your hook:
add_filter('single_template','yoursite_single_template',11);
function yoursite_single_template($template) {
if (get_query_var('cpage'))
$template = str_replace('.php','[comments].php',$template);
return $template;
}
And here's the proof that it all works!
(source: mikeschinkel)
本文标签: Stop WordPress redirecting commentpage1 to the post page
版权声明:本文标题:Stop WordPress redirecting comment-page-1 to the post page? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745501772a2661065.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论