WordPress虚拟页面呈现为博客而不是页面

时间:2019-01-28 06:37:44

标签: wordpress templates

我在使用generate_rewrite_rules生成虚拟页面的类中遇到问题...我使用过滤器the_content而不是template_redirecttemplete_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;
    }
}

1 个答案:

答案 0 :(得分:0)

解决方案是修改$wp_query变量

public function resetQuery( $query )
{
    global $wp_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');
        $wp_query->is_page = TRUE;
        $wp_query->is_singular = TRUE;
        $wp_query->is_home = FALSE;
        $wp_query->is_archive = FALSE;
        $wp_query->is_category = FALSE;
        unset($wp_query->query['error']);
        $wp_query->query_vars['error'] = '';
        $wp_query->is_404 = FALSE;
    }
}