У меня проблема в классе, который генерирует виртуальные страницы, используя generate_rewrite_rules
... Я использую фильтр the_content
вместо template_redirect
или templete_include
, поэтому с содержимым все в порядке, независимо от темы / сайта.
Виртуальная страница почти работает ... Я вижу, что моя страница загружена, но страница загружается как страница архивного блога, а не как отдельная страница.Как я могу заставить WordPress визуализировать страницу как страницу?
Вот класс:
<?php
namespace Magana\CShorts\Components\VP;
use Magana\Plugins\Core\Configuration;
defined( 'ABSPATH' ) || exit;
class SingleContact
{
public function __construct()
{
add_filter( 'generate_rewrite_rules', [$this,'rewriteRules'] );
add_filter( 'query_vars', [$this,'queryVars'] );
add_action( 'pre_get_posts', [$this,'resetQuery'], 1 );
add_filter( 'the_content', [$this, 'contactContent'] );
add_filter( 'wp_title', [$this, 'contactTitle'] );
add_filter( 'the_title', [$this, 'hideTitle'] );
}
public function rewriteRules( $wp_rewrite )
{
$wp_rewrite->rules = array_merge(
['country/(.*?)/(.*?)/?$' => 'index.php?country=$matches[1]&contact_id=$matches[2]'],
$wp_rewrite->rules
);
}
public function queryVars( $query_vars )
{
$query_vars[] = 'country';
$query_vars[] = 'contact_id';
return $query_vars;
}
public function resetQuery( $query )
{
$id = get_query_var( 'contact_id' );
$country = get_query_var( 'country' );
if($id && $country && $query->is_main_query()){
$page_id = url_to_postid('hex-single-contact');
$query->set( 'post__in', [$page_id] );
$query->set( 'posts_per_page', '1' );
$query->set('post_type', 'page');
$query=[];
}
}
function contactTitle($title)
{
$id = get_query_var( 'contact_id' );
$country = get_query_var( 'country' );
if ($id && $country){
return __("Contact");
}
return $title;
}
function contactContent($content)
{
$id = get_query_var( 'contact_id' );
$country = get_query_var( 'country' );
if ($id && $country){
$ga = Configuration::get("ga");
$new_template = locate_template(['single-contact.php']) ?: HEX_CSHORTS_ABSPATH . '/App/templates/single-contact.php';
if (!$new_template) {
wp_die("Template 'single-contact' not found");
}
require $new_template;
return false;
}
return $content;
}
public function hideTitle( $title ) {
$id = get_query_var( 'contact_id' );
$country = get_query_var( 'country' );
if ($id && $country && in_the_loop())
$title = false;
return $title;
}
}