問題描述

我想在沒有父帖子的自己的頁面上顯示發表評論。我知道我可以在單個帖子上使用 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:

http://example.com/comments/%comment_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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。