問題描述
我想在沒有父帖子的自己的頁面上顯示發表評論。我知道我可以在單個帖子上使用 wp_list_comments(),並傳遞迴調函式以使用我自己的評論顯示標記。我打算這樣做,所以我可以包括一個連結,每個評論將顯示它自己的頁面上的評論。
如果這不是 WordPress 我會使用:
<a href = " www.example.com/individual_comment.php?comment_id = $comment_id">View single comment</a>
… 並從查詢字串中獲取 $ comment_id 。
WordPress 中的連結會是什麼樣子?即:我要包括什麼字串來直接訪問,我們說 my_comments.php 我在哪裡呼叫 get_comment($ comment_id) 和 comment_template()?
<a href = "<?php bloginfo('url');?>/what/goes/here?comment_id = $comment_id"<View single comment</a>
最佳解決方法
您可能只需在 WordPress 中建立一個新頁面,併為該頁面提供自定義模板。那麼 url 將是通常會到達該頁面的任何東西。唯一的區別是,您使用的自定義模板將被設定為透過 querystring 接受 comment_id,然後只需獲取特定註釋的詳細資訊,並在模板程式碼中回顯註釋的詳細資訊。
所以,如果您建立了一個名為”Comment Details” 的 wordpress 頁面,您可以透過 http://www.domain.com/comment-details 訪問該頁面 (假設您已啟用固定連結) 。所以你的連結看起來像這樣:
<a href = "<?php bloginfo('url');?>/comment-details?comment_id=$comment_id">View single comment</a>
“Comment Details” 頁面將被設定為使用一個自定義模板,其中包含程式碼以吐出細節。
次佳解決方法
有許多不同的方法來實現這一點,一些比其他的更好,幾乎所有這些都有可能與其他外掛衝突,但忽略所有這一切都非常接近你所要求的一切。 🙂
此解決方案將支援以下 URL 格式,其中%comment_id% 是 wp_comments 表的註釋的數字 ID:
首先,您需要使用以下程式碼配置 URL 重寫。希望它是合理的 self-explanitory 但毫不猶豫地問:
$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.
}
}
接下來,您將需要使用 template_include 過濾器將程式碼鉤在/wp-includes/template-loader.php 中。這將在 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="http://<?php%20echo%20$comment->comment_author_url;%20?>"><?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="http://<?php%20echo%20$permalink;%20?>"><?php echo $post->post_title; ?></a></p>
<blockquote><?php echo $comment->comment_content; ?></blockquote>
</div>
</div>
<?php
get_sidebar();
get_footer();
任何問題?只是問
附:我上面描述的所有程式碼都可以在主題的 functions.php 檔案和/或您自己的外掛中。一個注意事項是 URL 重寫重新整理規則,應該在外掛啟用鉤子中,如果你要包括它,而不是我們在管理控制檯的 「永久連結」 部分手動重新整理它們。我沒有顯示如何註冊一個啟用鉤做,但如果你想了解更多,你可以閱讀 here 。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。