【问题标题】:Wordpress comments on dynamically created pages动态创建页面上的 Wordpress 评论
【发布时间】:2012-08-11 18:49:39
【问题描述】:

我在 Wordpress 中有一个带有自定义模板的自定义页面。除了向用户显示实际页面外,它还通过传入的 URL 变量动态创建页面。

所以我在 wordpress 中创建了一个名为 News 的页面并将其分配给我的 news.php 模板。用户可以点击 mywebsite.com/news 上的页面

他们还可以转到 mywebsite.com/news/2012/august-8/,页面模板通过这些 url 变量读取日期,并仅显示该页面的新闻。

这就是我想要做的。我想将 cmets 添加到“日期特定页面”中,这些页面不是 wordpress 中的实际页面,而是基于 url 变量动态创建的。

我可以将 cmets_template() 添加到页面,但我相信它是基于页面 id 或帖子 id...有没有办法插入自定义 id 或插入 url 来为这些动态页面创建 cmets?

我不希望 mywebsite.com/news/2012/august-8/ 上的 cmets 出现在 mywebsite.com/news/2012/august-9/ 上——它们是单独的页面

想法?

【问题讨论】:

  • 有没有办法创建一个唯一的 ID(因为我假设 wordpress 通常将评论链接到帖子/页面 ID),然后让 wordpress 为这些自定义页面存储带有自定义 ID 的评论?
  • 我有一个类似的问题,我想在没有帖子或页面 ID 的动态创建的页面上显示 cmets。您最终找到解决方案了吗?

标签: php wordpress comments


【解决方案1】:

我理解你,但我认为以这种方式使用 WP 不是一个好主意。您正在搞乱 WP、他们的帖子和 cmets 的主要功能。我应该为这项工作使用另一个框架。 wp_commentmeta table 和他们的朋友是一个很好的调查起点:

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

确保您可以实现您所需要的。

【讨论】:

    【解决方案2】:

    我知道这个问题很老,但无论如何我都会发布答案,以防人们仍在寻找。老实说,我认为这个功能应该包含在创建动态用户配置文件类型页面的插件中,我不确定开发人员为什么要添加这个功能。

    只要它们是动态页面中唯一的 url 的一部分,此方法就可以工作。就我而言,我使用网址的第二部分。

    1. 首先,您要向 cmets 添加一个额外的隐藏注释字段,该字段获取部分或全部 url 并将其填充到文本框中。您可以对网站上的所有 cmets 执行此操作,也可以像我一样使用 if 查询,如果您只在一个父页面上需要它。

    2. 接下来,您可以确保将此文本框作为额外的评论元数据发布到您的 wp_commentmeta 表中。现在,发布到特定 url 的每条评论都有一个唯一的 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 部分匹配的元值的 cmets。这将进入您的父帖子页面模板,即在本示例页面 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-18
      • 1970-01-01
      • 2016-12-11
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多