2012-08-11 54 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>