Wordpress 在新页面上显示评论?
我需要在每个评论后显示链接,当你点击该链接时,一个新页面会在一个新页面上显示该单一内容 有可能吗?(OP评论后的新编辑版本) 有很多方法可以做到这一点。理论上,这是最简单的,但可能不是“根据WordPress”的方式最合适的。以此为出发点。我还没有测试它,所以您可能会遇到一两个错误,通过一些小的调整应该可以解决。如果你被难倒了,请告诉我,我会尽力的。所以在概念上 您应该复制page.php模板文件并将其重命名为“comments\u page.php”(或任何您喜欢的名称)。在代码编辑器中打开此文件,并找到以下显示位置:(如果不存在,则创建它) 并将其更改为Wordpress 在新页面上显示评论?,wordpress,comments,Wordpress,Comments,我需要在每个评论后显示链接,当你点击该链接时,一个新页面会在一个新页面上显示该单一内容 有可能吗?(OP评论后的新编辑版本) 有很多方法可以做到这一点。理论上,这是最简单的,但可能不是“根据WordPress”的方式最合适的。以此为出发点。我还没有测试它,所以您可能会遇到一两个错误,通过一些小的调整应该可以解决。如果你被难倒了,请告诉我,我会尽力的。所以在概念上 您应该复制page.php模板文件并将其重命名为“comments\u page.php”(或任何您喜欢的名称)。在代码编辑器中打开此
/*Template Name: comments_page*/
现在打开WordPress管理区,创建一个新页面。随便你怎么称呼它,但不要添加任何内容。在右侧列中,从“页面模板”下拉菜单中选择页面使用的模板。选择“评论页面”(或模板名称中列出的任何内容)。这会告诉WordPress使用您的文件来显示此特定页面,而不是默认页面模板。保存页面并记下WordPress生成的页面id
现在,找到主题的注释模板,通常是“comments.php”。查找函数wp_list_comments()代码>。我们将添加一个自定义函数的名称,该函数将控制注释的显示,作为此函数的参数。例如,转到twenty-ten主题的文件,打开comments.php,您将看到它的样子:
wp_list_comments( array( 'callback' => 'twentyten_comment' ) );
打开Twent-ten主题的functions.php并查找
function twentyten_comment()
复制整个函数并将其粘贴到主题的函数文件中。将名称更改为“my_comment()”,并将其添加到wp_list_comments函数调用中,如下所示:
wp_list_comments( array('callback'=>'my_comment'));
在functions.php文件中新创建的“my_comment()”函数中,添加一个链接,使用名为“commentID”的查询变量和注释ID,将其链接到单独的注释页面(comments_page.php)
<a href = "<?php echo get_page_link($page_id).'/?commentID='.comment_ID();?>">View this comment</a>
现在,$comment变量中的所有单个注释信息都作为对象存在
您可以决定如何显示注释,但首先,我建议复制主题注释模板的内容以保持一致。它将显示与post页面显示的内容完全相同的内容,但听起来此页面更适合于永久链接到您从其他地方链接到的单个注释
希望这有帮助。如果你遇到困难,请告诉我
注意:此答案提供了我在上得到的信息。我昨天刚刚在WordPress答案(也是StackExchange网站)上回答了您的确切问题。你可以找到答案。它包括以下四个步骤:
通过添加query\u var
、rewrite\u标记和permastruct
来设置URL重写
确保刷新插件激活挂钩中的重写规则或手动刷新
添加一个parse\u query
filter钩子,将query\u vars
的帖子设置为评论帖子,并为查询禁用粘贴帖子
添加template\u include
filter钩子以过滤模板文件名,为单个注释加载特定于模板的模板文件,最后
将注释模板文件创建为/wp content/themes/%your theme%/comment.php
同样,你可以找到答案
希望这有帮助
-迈克
更新:
以下是我在WordPress答案上发布的全部内容:
有许多不同的方法可以实现这一点,有些方法比其他方法更加精巧,几乎所有的方法都有可能与其他插件发生冲突,但忽略所有这些,这里有一种方法非常接近您所要求的:
此解决方案将支持如下URL格式,其中%comment\u id%
是wp\u comments
表中注释的数字id:
首先,您需要使用以下代码配置URL重写。希望这是一种合理的自我嘲讽,但请毫不犹豫地问:
$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.
您还需要在插件激活挂钩中调用此代码以刷新规则,或者如果是您的站点,您可以在管理控制台的“设置”>“永久链接设置”区域中保存永久链接:
global $wp_rewrite;
$wp_rewrite->flush_rules(false);
接下来添加一个parse\u查询
filter钩子。这将在WordPress检查查询后调用。它测试您添加的comment\u id
query\u var是否已设置,如果已设置,则测试您是否位于所需的URL上。如果是,则使用get_comment()
加载注释数组,以便将'p'
参数(应设置为帖子ID)设置为与注释相关的帖子。这样,当WordPress运行它将要运行的查询时,不管发生什么,它至少会在下面的comment.php
主题模板文件中加载您需要的内容,并且您以后不必在需要时运行另一个查询。此代码还告诉WordPress使用奇怪的caller\u get\u posts
选项忽略粘性帖子:
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.
}
}
接下来还需要使用template\u include
过滤器将代码挂接到/wp includes/template loader.php
。WordPress检查查询并加载评论帖子后,将调用此函数。在这里,您将首先再次检查查询变量中的comment\u id
,以及您想要的URL。如果是这样,我们将/index.php
模板页面替换为/comment.php
,这是您需要创建的主题模板文件:
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;
}
最后,现在您需要创建主题模板文件,我选择调用/comment.php
。因为这是你的主题,你会想让它看起来像你想要的,但这里有一个例子让你
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.
}
}
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;
}
<?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();
<?php
if ( ! function_exists( 'twentyten_comment' ) ) :
function my_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case '' :
?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="comment-author vcard">
<?php echo get_avatar( $comment, 40 ); ?>
<?php printf( __( '%s <span class="says">says:</span>', 'twentyten' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
</div><!-- .comment-author .vcard -->
<?php if ( $comment->comment_approved == '0' ) : ?>
<em><?php _e( 'Your comment is awaiting moderation.', 'twentyten' ); ?></em>
<br />
<?php endif; ?>
<div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
<?php
/* translators: 1: date, 2: time */
printf( __( '%1$s at %2$s', 'twentyten' ), get_comment_date(), get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'twentyten' ), ' ' );
?>
</div><!-- .comment-meta .commentmetadata -->
<div class="comment-body"><?php comment_text(); ?></div>
<a href = "<?php echo get_page_link($page_id).'/?commentID='.comment_ID();?>">View this comment</a>
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
</div><!-- #comment-## -->
<?php
break;
case 'pingback' :
case 'trackback' :
?>
<li class="post pingback">
<p><?php _e( 'Pingback:', 'twentyten' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __('(Edit)', 'twentyten'), ' ' ); ?></p>
<?php
break;
endswitch;
}
endif;
/*Template Name: comments_page*/
<? if(isset($_GET['commentID'])){$commentID = $_GET['commentID'];}
$comment = get_comment($commentID);
?>
<?php get_header(); ?>
<div id="content">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<!--uncomment for header tags-- <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>
<small><b>Posted:</b> <?php the_time('F jS, Y') ?> | <b>Author:</b> <?php the_author_posts_link(); ?> | <b>Filed under:</b> <?php the_category(', ') ?> <?php the_tags(' | <b>Tags:</b> ', ', ', ''); ?> <?php if ( $user_ID ) :
?> | <b>Modify:</b> <?php edit_post_link(); ?> <?php endif; ?>| <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></small> -->
<?php the_content('Read the rest of this entry »'); ?>
<hr/>
</div>
<?php endwhile; ?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>
<div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
</div>
<?php else : ?>
<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php // Do not delete these lines
if (!empty($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
die ('Please do not load this page directly. Thanks!');
if (!empty($post->post_password)) { // if there's a password
if ($_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password) { // and it doesn't match the cookie
?>
<p class="nocomments">This post is password protected. Enter the password to view comments.</p>
<?php
return;
}
}
/* This variable is for alternating comment background */
$oddcomment = 'class="alt" ';
?>
<!-- You can start editing here. -->
<div id="comments">
<?php if ($comments) : ?>
<h3><?php comments_number('No Comments', 'One Comment', '% Comments' );?> on “<?php the_title(); ?>”</h3>
<?php wp_list_comments( array('callback'=>'my_comment')); ?>
<?php else : // this is displayed if there are no comments so far ?>
<?php if ('open' == $post->comment_status) : ?>
<!-- If comments are open, but there are no comments. -->
<?php else : // comments are closed ?>
<!-- If comments are closed. -->
<p class="nocomments">Comments are closed.</p>
<?php endif; ?>
<?php endif; ?>
<?php if ('open' == $post->comment_status) : ?>
<hr/>
<h4 class="center">Leave a Reply</h4>
<?php if ( get_option('comment_registration') && !$user_ID ) : ?>
<p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">logged in</a> to post a comment.</p>
<?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<ul class="formlist">
<?php if ( $user_ID ) : ?>
<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="Log out of this account">Log out »</a></p>
<?php else : ?>
<li><input type="text" name="author" id="author" value="Name <?php if ($req) echo "(required)"; ?>" size="22" tabindex="1" <?php if ($req) echo "aria-required='true'"; ?> onblur="if(this.value.length == 0) this.value='Name <?php if ($req) echo "(required)"; ?>';" onclick="if(this.value == 'Name <?php if ($req) echo "(required)"; ?>') this.value='';" /></li>
<li><input type="text" name="email" id="email" value="Mail (will not be published) <?php if ($req) echo "(required)"; ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?> onblur="if(this.value.length == 0) this.value='Mail (will not be published) <?php if ($req) echo "(required)"; ?>';" onclick="if(this.value == 'Mail (will not be published) <?php if ($req) echo "(required)"; ?>') this.value='';" /></li>
<li><input type="text" name="url" id="url" value="Website" size="22" tabindex="3" onblur="if(this.value.length == 0) this.value='Website';" onclick="if(this.value == 'Website') this.value='';" /></li>
<?php endif; ?>
<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->
<li><textarea name="comment" id="comment" cols="70%" rows="10" tabindex="4" value="Enter comment here."></textarea></li>
<li class="submitbutton"><input name="submit" type="submit" id="submit" tabindex="5" value="Submit Comment" /></li>
<input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" />
<?php do_action('comment_form', $post->ID); ?>
</ul>
</form>
<?php endif; // If registration required and not logged in ?>
<?php endif; // if you delete this the sky will fall on your head ?>
</div>