[WordPress] 建立不存在 WP_POSTS 資料表中的「假文章/頁面」

本篇文章更新時間:2020/12/25
如有資訊過時或語誤之處,歡迎使用 Contact 功能通知。
一介資男的 LINE 社群開站囉!歡迎入群聊聊~


這個技巧是今天給 Revolution Slider 這款外掛預覽功能偵錯時發現的。

它的預覽功能原理是使用特定的樣板(template)搭配使用內建 The Loop 機制來載入讀取文章內容。

其中會使用一個「建立假文章/頁面」的方法,來符合輸出一個 WordPress Post Object,滿足內建方法載入頁面內容。

<?php
function create_fake_post($content, $title = 'Slider Revolution') {
    $post                 = new stdClass();
    $post->ID             = -1;
    $post->post_author    = get_current_user_id();
    $post->post_date      = current_time('mysql');
    $post->post_date_gmt  = current_time('mysql', 1);
    $post->post_title     = $title;
    $post->post_content   = $content;
    $post->post_status    = 'publish';
    $post->comment_status = 'closed';
    $post->ping_status    = 'closed';
    $post->post_name      = 'rs-fake-page-' . rand(1, 99999); //append random number to avoid clash
    $post->post_type      = 'page';
    $post->filter         = 'raw'; //important

    //$post->post_meta              = new stdClass();
    //$post->post_meta->_wp_page_template= '../public/views/revslider-page-template.php';

    //Convert to WP_Post object
    $wp_post = new WP_Post($post);
    //Add the fake post to the cache
    wp_cache_add(-1, $wp_post, 'posts');

    global $wp, $wp_query;

    // Update the main query
    $wp_query->queried_object_id    = -1;
    $wp_query->post                 = $wp_post;
    $wp_query->posts                = array($wp_post);
    $wp_query->queried_object       = $wp_post;
    $wp_query->found_posts          = 1;
    $wp_query->post_count           = 1;
    $wp_query->max_num_pages        = 1;
    $wp_query->is_page              = true;
    $wp_query->is_singular          = true;
    $wp_query->is_single            = false;
    $wp_query->is_attachment        = false;
    $wp_query->is_archive           = false;
    $wp_query->is_category          = false;
    $wp_query->is_tag               = false;
    $wp_query->is_tax               = false;
    $wp_query->is_author            = false;
    $wp_query->is_date              = false;
    $wp_query->is_year              = false;
    $wp_query->is_month             = false;
    $wp_query->is_day               = false;
    $wp_query->is_time              = false;
    $wp_query->is_search            = false;
    $wp_query->is_feed              = false;
    $wp_query->is_comment_feed      = false;
    $wp_query->is_trackback         = false;
    $wp_query->is_home              = false;
    $wp_query->is_embed             = false;
    $wp_query->is_404               = false;
    $wp_query->is_paged             = false;
    $wp_query->is_admin             = false;
    $wp_query->is_preview           = false;
    $wp_query->is_robots            = false;
    $wp_query->is_posts_page        = false;
    $wp_query->is_post_type_archive = false;

    //Update globals
    $GLOBALS['wp_query'] = $wp_query;
    $wp->register_globals();

    return $wp_post;
}

Gist: Link

這個假文章的技術,不是所謂的 Lorem Ipsum,而是從頭到尾都不存在資料庫裡的內容。

這方法應用情境也不同,像是特別隨著瀏覽互動改變內容的 Demo 頁面,或是源頭就不經過 WordPress 內容資料表 wp_posts 的內容,只利用 WordPress 樣版引擎去發揮的網站系統等。


Share:

作者: Chun

資訊愛好人士。主張「人人都該為了偷懶而進步」。期許自己成為斜槓到變進度條 100% 的年輕人。[//////////____30%_________]

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *