我需要在每条评论后显示链接,当您点击该链接时,新页面会在新页面上显示该单个广告。
这可能吗?
答案 0 :(得分:2)
我昨天在WordPress Answers(也是StackExchange网站)上回答了你的确切问题。你可以找到答案here。它涉及以下四个步骤:
query_var
,rewrite_tag
和permastruct
,parse_query
过滤器挂钩以将query_vars
的帖子设置为评论的帖子并禁用查询的粘贴帖子,template_include
过滤器挂钩以过滤模板文件名,以便为单个注释加载模板特定模板文件,最后/wp-content/themes/%your-theme%/comment.php
。同样,您可以找到答案over here。
希望这有帮助。
-Mike
以下是我在WordPress Answers上发布的完整内容:
有许多不同的方法可以实现这一目标,有些方法比其他方法更精致,几乎所有方法都有可能与其他插件发生冲突,但忽略了所有这些与你所要求的方式非常接近的方式。 :)
此解决方案将支持以下网址格式,其中%comment_id%
是wp_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_query
过滤器挂钩。这将在WordPress检查查询后调用。它会测试您是否添加了comment_id
query_var设置,如果是这样,它会测试您是否在所需的URL上。如果是,则它使用get_comment()
加载注释数组,以便将'p'
参数(应设置为帖子ID)设置为与注释相关的帖子。这样,当WordPress运行它将要运行的查询时,无论至少它在下面的comment.php
主题模板文件中加载了您需要的内容,您将不必稍后在需要时运行另一个查询。此代码还告诉WordPress使用奇怪命名的caller_get_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.
}
}
接下来,您需要使用/wp-includes/template-loader.php
过滤器将代码挂钩到template_include
。这将在WordPress检查查询并加载评论的帖子后调用。在这里,您将首先再次检查query_var中的comment_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
的主题模板文件。既然这是你的主题,你会想让它看起来像你想要的,但这里是一个让你入门的例子:
<?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();
有任何问题吗?请问。
希望这有帮助。
P.S。我上面描述的所有代码都可以放在主题的functions.php
文件中和/或你自己的插件中。需要注意的是URL重写刷新规则应该放在插件激活挂钩中,如果你要包含它而不是我们只是在管理控制台的固定链接部分手动刷新它们。我没有展示如何注册激活钩子但是如果你想了解更多,你可以阅读它here。
答案 1 :(得分:0)
(OP评论后的新编辑版)
有很多方法可以做到这一点。理论上这是最简单的,但根据WordPress的方式可能不是“最合适的”。以此为出发点。我没有对它进行过测试,所以你可能会遇到一两个错误,这些错误应该可以通过一些小的调整来解决。如果你感到难过,请告诉我,我会尽我所能。所以概念上......
您应该复制page.php模板文件并将其重命名为'comments_page.php'(或您喜欢的任何内容)。在代码编辑器中打开此文件,找到出现以下内容的位置:(如果不存在,则创建它)
/*Template Name: page*/
并将其更改为
/*Template Name: comments_page*/
现在打开您的WordPress管理区域并创建一个新页面。无论你想要什么,都可以调用,但不要添加任何内容。在右侧列中,从“页面模板”下拉菜单中选择页面使用的模板。选择“comments_page”(或您列出的任何模板名称)。这告诉WordPress使用您的文件来显示此特定页面而不是默认页面模板。保存页面并记下WordPress生成的page_id。
现在,找到你主题的评论模板,通常是'comments.php'。找到函数wp_list_comments();
。我们将添加一个自定义函数的名称,该函数将控制注释的显示作为此函数的参数。例如,转到二十个主题的文件,打开comments.php,你会看到它的样子:
wp_list_comments( array( 'callback' => 'twentyten_comment' ) );
打开二十个主题的functions.php并找到
function twentyten_comment()
复制整个函数并将其粘贴到主题的函数文件中。将名称更改为“my_comment()”,并将其添加到wp_list_comments函数调用中,如下所示:
wp_list_comments( array('callback'=>'my_comment'));
在functions.php文件中新创建的“my_comment()”函数中,使用get_page_link()和名为'的查询var添加一个链接,指向单独的注释页面(comments_page.php) commentID'和评论ID。
<a href = "<?php echo get_page_link($page_id).'/?commentID='.comment_ID();?>">View this comment</a>
现在不恰当地将php逻辑添加到模板文件中。一旦你理解了它是如何工作的,你可以在functions.php文件中创建一个函数,然后在主题文件中调用它...
在comments_page.php上,使用$ _GET ['commentID']从网址中检索评论的id值,并将其传递给get_comment($commentID)以检索单个评论并将其显示在单个页面上。
if(isset($_GET['commentID'])){$commentID = $_GET['commentID'];}
$comment = get_comment($commentID);
现在,您将$ comment变量中的所有单个注释信息作为对象。
您可以决定如何显示评论,但是为了开始,我建议您复制主题评论模板的内容以保持一致。它将显示帖子页面显示的完全相同的内容,但听起来这个页面更适用于您从其他地方链接到的单个评论的永久链接。
希望这会有所帮助。如果你遇到麻烦,请告诉我。
给我的信息答案 2 :(得分:0)
这是我的functions.php
<?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;
这是我的comments_page.php
/*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
<?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>