2012-08-11 56 views
3

我有一個自定義模板的WordPress的自定義頁面。除了向用戶顯示實際頁面之外,它還通過傳遞URL變量動態創建頁面。對動態創建頁面的WordPress評論

因此,我在wordpress中創建了一個名爲News的頁面,並將其分配給我的news.php模板。用戶可以點擊頁面mywebsite.com/news

他們也可以轉到mywebsite.com/news/2012/august-8/和頁面模板通過這些url變量讀取日期,並顯示新聞的只是頁。

這是我想要做的。我想爲「日期特定頁面」添加評論,這些頁面不是WordPress內部的實際頁面,而是基於url變量實時創建的。

我可以添加comments_template()到頁面,但我相信它是基於頁面ID或帖子ID ...有沒有辦法插入自定義ID或插入URL來創建這些動態頁面的評論?

我不想上mywebsite.com/news/2012/august-8/的評論顯示在mywebsite.com/news/2012/august-9/ ---它們是獨立的頁面

想法?

+0

有沒有一種方法來創建一個唯一的ID(因爲我假設wordpress的一般評論鏈接到文章/頁面ID),然後讓WordPress的存儲這些自定義頁面的自定義ID的評論? – RailsTweeter 2012-08-11 18:55:28

+0

我有一個類似的問題,我想在沒有帖子或頁面ID的動態創建的頁面上顯示評論。你最終是否找到了解決方案? – Swen 2015-10-24 04:04:55

回答

0

我明白你的意思,但我認爲這樣做並不是好主意。你在搞亂WP的主要功能,他們的帖子和評論。我應該使用另一個框架來完成這項工作。任何一個很好的起點,調查是wp_commentmeta表和他們的朋友:

add_comment_meta($comment_id, $meta_key, $meta_value, $unique = false) 
get_comment_meta($comment_id, $meta_key, $single = false) 

當然,你可以實現你所需要的。

0

我知道這個問題已經很老了,但是我會發布答案,無論如何人們仍在尋找。老實說,我覺得這個功能應該包括與創建動態的用戶資料類型的網頁插件,我不知道爲什麼開發商已經加入此功能。

只要他們是在動態獨特的URL的一部分這個方法的頁面將工作。在我的情況下,我使用url的第二部分。

  1. 首先,你要一個額外的隱藏註釋字段添加到您的意見,抓住URL的一部分或全部,並填充它在文本框中。您可以對網站上的所有評論執行此操作,也可以像我一樣使用if查詢,如果您只在一個父頁面上需要它。

  2. 接下來,您可以確保將此文本框作爲額外評論元數據發佈到您的wp_commentmeta表中。現在,您可以爲發佈到可以查詢數據庫的特定網址的每條評論提供唯一的ID。

    // adds the extra comment field and populates it with the second part 
        // of the the url for example (mydomain.com/profile/userid -> $lastpart[2] = userid) 
        // also posts data to database. the ability to edit is there in case you want to show the element 
        // this part would go in your functions.php 
    
         function unique_comments_additional_field() { 
         global $post_id; 
         if(is_page(108)){ 
    
          $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
          $path = parse_url($url, PHP_URL_PATH); 
          $lastpart = explode('/', rtrim($path, '/')); 
    
          echo '<p style="display:none;">'. 
          '<input id="hiddencoment" name="hiddencoment" placeholder="' . __('hiddencoment', 'Extra Field') . '" value="'.$lastpart[2].'" type="text" size="30" /></p>'; 
        } 
    
         add_action('comment_form_logged_in_after', 'unique_comments_additional_field'); 
         add_action('comment_form_after_fields', 'unique_comments_additional_field'); 
    
    
         function save_comment_meta_data($comment_id) { 
    
          if ((isset($_POST['hiddencomment'])) && ($_POST['hiddencomment'] != '')) { 
           $hiddencomment = wp_filter_nohtml_kses($_POST['hiddencomment']); 
           add_comment_meta($comment_id, 'hiddencomment', $hiddencomment); 
          } 
    
         } 
         add_action('comment_post', 'save_comment_meta_data'); 
    
         function unique_extend_comment_add_meta_box() { 
          add_meta_box('hiddencomment', __('Comment Metadata', 'Extra Field'), 'unique_extend_comment_meta_box', 'comment', 'normal', 'high'); 
         } 
         add_action('add_meta_boxes_comment', 'unique_extend_comment_add_meta_box'); 
    
         function unique_extend_comment_meta_box($comment) { 
          $hiddencomment = get_comment_meta($comment->comment_ID, 'hiddencomment', true); 
          wp_nonce_field('extend_comment_update', 'extend_comment_update', false); 
          ?> 
          <p> 
          <input type="text" style="display:none;" name="hiddencomment" value="<?php echo esc_attr($hiddencomment); ?>" class="widefat" /> 
          </p> 
          <?php 
         } 
    
         function unique_extend_comment_edit_metafields($comment_id) { 
          if(! isset($_POST['extend_comment_update']) || ! wp_verify_nonce($_POST['extend_comment_update'], 'extend_comment_update')) return; 
    
          if ((isset($_POST['hiddencomment'])) && ($_POST['hiddencomment'] != '')): 
          $hiddencomment = wp_filter_nohtml_kses($_POST['hiddencomment']); 
          update_comment_meta($comment_id, 'hiddencomment', $hiddencomment); 
          else : 
          delete_comment_meta($comment_id, 'hiddencomment'); 
          endif; 
         } 
         add_action('edit_comment', 'unique_extend_comment_edit_metafields'); 
    

查詢表明,具有與剛剛保存到commentmeta表的URL的一部分,一個meta值評論。這會去你的父母后的網頁模板,即在這個例子中第108頁

<ol class="commentlist" style="list-style: none; background: #eee; padding: 10px;"> 
      <?php 

       $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
       $path = parse_url($url, PHP_URL_PATH); 
       $lastpart = explode('/', rtrim($path, '/')); 
       $customer_id = (string)$lastpart[2]; 

       //Gather comments for a specific page/post 
       $comments = get_comments(array('meta_key' => 'title', 'meta_value' => $customer_id)); 


       //Display the list of comments 
       wp_list_comments(array(
        'per_page' => 10, //Allow comment pagination 
        'reverse_top_level' => true //Show the latest comments at the top of the list 
       ), $comments); 

      ?> 
     </ol>