虚拟页面上的自定义字段

时间:2014-04-22 作者:Linda H

我使用了DaveJesch的解决方案来创建虚拟页面(从临时保存在自定义数据库表中的内容,每四个小时从RSS更新一次)。查看更多信息:http://davejesch.com/wordpress/wordpress-tech/creating-virtual-pages-in-wordpress/

到目前为止,我已经验证了url看起来像一个虚拟页面(也就是说,它是某个slug的子页面,它包含数字作为slug的第一部分,当然这是特定于我的网站的)。我已经从自定义DB表中获取了创建页面所需的所有内容。我已经设法使用了一个自定义模板。

我尚未解决并需要帮助的问题是,如何在页面中添加(伪造)自定义字段,而不将其添加到数据库中。我想在自定义表之外使用一些值the_content() 但在循环中,就像自定义字段一样。

我看到的所有建议都将数据作为自定义字段保存到数据库中,保存到页面id-1中,但由于该网站有相当多的访问者,并且所有虚拟页面都将获得id-1,这只会导致糟糕的结局,另外,我不想在数据库中保存一堆垃圾,因为我的自定义数据库的内容会经常更改(它们是招聘广告,所以会有数百个,而且它们很少在系统中停留超过几周,最多)。

那么,有没有一种方法可以将假自定义字段与假页面一起发送,或者有没有更好的方法呢?

预计到达时间:

下面的代码是该函数的大部分组成部分。更改了一些函数名,使其不太特定于客户端,但总的来说,仅此而已。这个get_ip_post() 函数被省略,因为它只从自定义表中获取内容并将其作为数组返回。

/********************************************/
/*                                          */
/* Virtual page, class and functions.       */
/*                                          */
/********************************************/

// Based on: http://davejesch.com/wordpress/wordpress-tech/creating-virtual-pages-in-wordpress/

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);
                echo $args[\'post_parent_ID\'];
            $this->type = isset($args[\'type\']) ? $args[\'type\'] : \'page\';
                echo $this->post_parent_ID;

            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_type = $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;
            } else { 
            }

            return ($posts);
        }
    }
}

function isJobAd($url){
        global $virtual_post_id;
        if( strpos( $url, \'lediga-uppdrag/\' ) > 0 || strpos( $url, \'ledige-oppdrag/\' ) > 0 ){   // Check that we\'re in the right place, Swedish & Norwegian urls respectively
            $url_parts = explode( \'/\', $url );
            $slug = end($url_parts);
            $post_id = current(explode("-", $slug));
            if(is_numeric($post_id)){
                $virtual_post_id = $post_id;
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    return true;
}

function dj_create_virtual() {
    global $current_blog;
    global $ip_slug;

    if( !is_admin() ) {
        $url = trim(parse_url($_SERVER[\'REQUEST_URI\'], PHP_URL_PATH), \'/\');

        $url_parts = explode( \'/\', $url );
        $slug = end($url_parts);
        $post_id = current(explode("-", $slug));

        if (isJobAd($url))
        {

            $arr_post = get_ip_post( $post_id );
            if( is_array( $arr_post ) ) {
                $args = array(\'slug\' => $ip_slug . \'/\' . $slug,
                        \'title\' => $arr_post[\'title\'],
                        \'content\' => $arr_post[\'description\'],
                        \'type\' => \'page\',
                        \'date\' => $arr_post[\'pubdate\']);

                $pg = new DJVirtualPage($args);
            } else {
                echo \'No post found.\';
            }
        }
    }
}
add_action(\'init\', \'dj_create_virtual\');


// Using page-ip-post.php as template
add_filter( \'single_template\', \'portfolio_page_template\', 99 );
function portfolio_page_template( $template ) {

    if ( is_page( -1 )  ) {
        $new_template = locate_template( array( \'page-ip-post.php\' ) );
        if ( \'\' != $new_template ) {
            return $new_template ;
        }
    }

    return $template;
}

1 个回复
最合适的回答,由SO网友:Milo 整理而成

如果您使用get_post_meta 在模板中,调用get_metadata 具有元类型post, 其中包含过滤器get_{$meta_type}_metadata, 在这种情况下get_post_metadata. 在那里,您可以检查密钥并返回所需的任何数据。

function wpd_fake_meta( $meta_data, $object_id, $meta_key, $single ){
    if( \'somekey\' == $meta_key ){
        return \'somefakevalue\';
    }
    return null;
}
add_filter( \'get_post_metadata\', \'wpd_fake_meta\', 20, 4 ); 

结束

相关推荐

Hide all posts by an author

我们有一个名为“技术”的类别,希望在此页面上隐藏特定作者的帖子。例如,约翰·史密斯的帖子不会显示在科技类,或任何与此相关的类别,甚至搜索中。我该怎么做?我不想隐藏作者姓名,我想从搜索、分类、分类页面等中完全隐藏作者X的所有帖子。非常感谢。