3

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

因此,我在 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/ 上——它们是单独的页面

想法?

4

2 回答 2

0

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

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

确保你能达到你所需要的。

于 2012-08-11T20:38:10.870 回答
0

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

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

  1. 首先,您想为您的评论添加一个额外的隐藏评论字段,该字段获取部分或全部 url 并将其填充到文本框中。您可以对网站上的所有评论执行此操作,也可以像我一样使用 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 部分匹配的元值的评论。这将进入您的父帖子页面模板,即在此示例页面 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>
于 2016-04-25T22:31:43.560 回答