动态创建页面的WordPress评论

vbkedwbf  于 2023-04-20  发布在  WordPress
关注(0)|答案(3)|浏览(180)

我有一个自定义的页面在WordPress的自定义模板。除了显示用户的实际页面,它还通过在URL变量传递动态创建页面。
因此,我在wordpress中创建了一个名为News的页面,并将其分配给我的news.php模板。mywebsite.com/news
他们也可以后藤mywebsite.com/news/2012/august-8/,页面模板通过这些url变量读取日期,并显示该页面的新闻。
这是我想做的,我想在“日期特定页面”中添加注解,这些页面并不是wordpress中的实际页面,而是基于url变量动态创建的。
我可以添加comments_template()到页面,但我相信它是基于页面ID或帖子ID的...有没有一种方法可以插入自定义ID或插入URL来为这些动态页面创建评论?
我不希望www.example.com上的评论mywebsite.com/news/2012/august-8/出现在www.example.com上mywebsite.com/news/2012/august-9/---它们是单独的页面
有什么想法

lskq00tm

lskq00tm1#

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

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

相信你能达到你所需要的。

cu6pst1q

cu6pst1q2#

我知道这个问题已经很老了,但我还是要把答案贴出来,以防人们还在寻找。老实说,我认为这个功能应该包含在创建动态用户配置文件类型页面的插件中,我不知道为什么开发人员要添加这个功能。
只要它们是动态页面中唯一的url的一部分,这个方法就可以工作。在我的例子中,我使用url的第二部分。
1.首先,你想在评论中添加一个隐藏的评论字段,它可以抓取部分或全部的url并填充到文本框中,你可以对你网站上的所有评论都这样做,或者你可以像我一样使用if查询,如果你只需要在一个父页面上使用它。
1.接下来,你可以确保这个文本框作为额外的注解元数据发布到你的wp_commentmeta表中。现在你有了一个唯一的id,每个发布到特定url的注解都可以查询数据库。

//   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' );

查询以显示具有 meta值的评论,该元值与我们刚刚保存到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>
xytpbqjk

xytpbqjk3#

@Rob:很棒的方法,非常感谢!在我遇到你的帖子之前,我已经花了几个小时来详细说明如何在动态生成的页面上进行评论。
只是几句话:

  • 这里有一个错字(“hiddencoment”而不是“hiddencomment”):

输入ID=“hiddencomment”name=“hiddencomment”placeholder="' . __('hiddencomment','Extra Field'). '”value=“'.$lastpart[2].'" type=“text”size=“30”

  • 我不得不将“title”改为“hiddencomment”才能正常工作:

$comments = get_comments(array(' meta_key' =〉'title','meta_value' =〉$customer_id));
再次感谢!最好的,简

相关问题