Отдельная страница для комментариев записи
Код ниже оформленный в виде плагина создает отдельную страницу комментариев записи на сайте. У каждой записи (поста) появляется новая страница .../comments/
на которой выводятся комментарии текущей записи.
Создаем файл плагина, назовем его comments-page-plugin.php
. Добавляем в него код ниже и подключаем файл в function.php
темы или как обычный плагин WordPress:
<?php /** * Plugin Name: Kama Post Comments Single Page * Description: Creates single page for post comments. * Version: 1.0 * Author: Kama * Author URI: http://wp-kama.ru */ $Kama_Separate_Comments_Page = new Kama_Separate_Comments_Page; $Kama_Separate_Comments_Page->init(); class Kama_Separate_Comments_Page { static $page_title_patt = "Comments for %s"; function init(){ add_filter( 'query_vars', [ $this, 'query_vars' ] ); add_action( 'init', [ $this, 'add_endpoint' ] ); add_action( 'single_template', [ $this, 'template_redirect' ] ); add_filter( 'get_comment_link', [ $this, 'get_comment_link' ] ); add_filter( 'wp_title', [ $this, 'wp_title' ], 10, 1 ); register_activation_hook( __FILE__, [ $this, 'activate'] ); register_deactivation_hook( __FILE__, [ $this, 'deactivate'] ); } function query_vars( $vars ){ $vars[] = 'comments'; return $vars; } # Add a /comments/ page to all post permalinks function add_endpoint(){ add_rewrite_endpoint( 'comments', EP_PERMALINK ); } # Template file for the /comments/ permalink function template_redirect( $templates = '' ){ global $wp_query; if( ! isset( $wp_query->query['comments'] ) ) return $templates; $templates = locate_template( 'comments-page.php', false ); if( ! $templates ){ $templates = __DIR__ . '/comments-page.php'; } return $templates; } # Fix comment permalinks function get_comment_link( $url ){ $urlparts = explode( '#', $url ); return untrailingslashit( $urlparts[0] ) . '/comments/#' . $urlparts[1]; } # Fix the page title function wp_title( $title ){ global $wp_query; if( isset( $wp_query->query['comments'] ) ) $title = sprintf( self::$page_title_patt, $title ); return $title; } function activate(){ $this->add_endpoint(); flush_rewrite_rules(); } function deactivate(){ flush_rewrite_rules(); } }
Теперь создаем файл шаблона. Он будет отвечать за вывод нашей страницы. Файл должен называться comments-page.php
и должен лежать в той же папке что и главный файл плагина.
Его код нужно будет подправить для вашей темы.
<?php get_header(); ?> <article id="post" class="hentry <?= post_class() ?>"> <h1 class="entry-title">Комментарии: "<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>"</h1> <div class="entry-content"> <?php wp_list_comments( 'reverse_top_level=false', get_comments([ 'post_id'=> $post->ID ]) ); comment_form(); ?> </div> </article> <?php get_footer();
Теперь нужно зайти на страницу админки «Постоянные ссылки», чтобы сбросить ЧПУ правила.
Все! Отдельная страница для комментариев записи готова. Добавляем к ссылке текущего поста в конец /comments/
и попадаем на страницу комментариев записи.