尝试创建虚拟页面时出现$wp_Query问题

时间:2014-06-24 作者:David Labbe

我正在尝试为我的插件创建一个虚拟页面,这样做时我遇到了$wp\\U查询重置问题。我正在使用这个类https://gist.github.com/brianoz/9105004 创建虚拟页面。我可以让一切正常工作,但问题是每当页面上有另一个循环时,无论是来自侧栏中的小部件插件,它都会从虚拟页面返回$wp\\u查询集。虚拟页面类已经接管了该页面,我找不到一种方法来实现它,因此页面上的任何其他查询都可以工作。谢谢

1 个回复
SO网友:David Labbe

我找到了这个,它很有效。最大的区别是,它检查$post count==0,并创建虚拟页面,然后允许所有其他查询正常运行。

<?php /*
Plugin Name: Virtual Page
Plugin URI: http://davejesch.com
Description: Create a virtual page with content
Author: Dave Jesch
Author URI: http://davejesch.com
Version: 1.0
*/

if (!class_exists(\'DJVirtualPage\'))
{
class DJVirtualPage
{
    private $slug = NULL;
    private $title = NULL;
    private $content = NULL;
    private $author = NULL;
    private $date = NULL;
    private $type = NULL;

    public function __construct($args)
    {
        if (!isset($args[\'slug\']))
            throw new Exception(\'No slug given for virtual page\');

        $this->slug = $args[\'slug\'];
        $this->title = isset($args[\'title\']) ? $args[\'title\'] : \'\';
        $this->content = isset($args[\'content\']) ? $args[\'content\'] : \'\';
        $this->author = isset($args[\'author\']) ? $args[\'author\'] : 1;
        $this->date = isset($args[\'date\']) ? $args[\'date\'] : current_time(\'mysql\');
        $this->dategmt = isset($args[\'date\']) ? $args[\'date\'] : current_time(\'mysql\', 1);
        $this->type = isset($args[\'type\']) ? $args[\'type\'] : \'page\';

        add_filter(\'the_posts\', array(&$this, \'virtualPage\'));
    }

    // filter to create virtual page content
    public function virtualPage($posts)
    {
        global $wp, $wp_query;

        if (count($posts) == 0 &&
            (strcasecmp($wp->request, $this->slug) == 0 || $wp->query_vars[\'page_id\'] == $this->slug))
        {
            //create a fake post intance
            $post = new stdClass;
            // fill properties of $post with everything a page in the database would have
            $post->ID = -1;                          // use an illegal value for page ID
            $post->post_author = $this->author;       // post author id
            $post->post_date = $this->date;           // date of post
            $post->post_date_gmt = $this->dategmt;
            $post->post_content = $this->content;
            $post->post_title = $this->title;
            $post->post_excerpt = \'\';
            $post->post_status = \'publish\';
            $post->comment_status = \'closed\';        // mark as closed for comments, since page doesn\'t exist
            $post->ping_status = \'closed\';           // mark as closed for pings, since page doesn\'t exist
            $post->post_password = \'\';               // no password
            $post->post_name = $this->slug;
            $post->to_ping = \'\';
            $post->pinged = \'\';
            $post->modified = $post->post_date;
            $post->modified_gmt = $post->post_date_gmt;
            $post->post_content_filtered = \'\';
            $post->post_parent = 0;
            $post->guid = get_home_url(\'/\' . $this->slug);
            $post->menu_order = 0;
            $post->post_tyle = $this->type;
            $post->post_mime_type = \'\';
            $post->comment_count = 0;

            // set filter results
            $posts = array($post);

            // reset wp_query properties to simulate a found 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;
        }

        return ($posts);
    }
}
}

function dj_create_virtual()
{
$url = trim(parse_url($_SERVER[\'REQUEST_URI\'], PHP_URL_PATH), \'/\');
if ($url == \'dave-virtual-page\')
{
    $args = array(\'slug\' => \'dave-virtual-page\',
            \'title\' => \'Dave\\\'s Virtual Page\',
            \'content\' => "This can be generated content, or static content<br />
        Whatever you put here will appear on your virtual page.");
    $pg = new DJVirtualPage($args);
}
}
add_action(\'init\', \'dj_create_virtual\');

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post