2013-12-19 40 views
4

我想製作一個博客網頁,根據他/她的Facebook喜歡,活動等爲特定用戶生成內容。例如,我喜歡Facebook上的夏奇拉和可口可樂。當進入博客並通過Facebook進行連接時,博客通過YouTube API獲取該信息並搜索Shakira的YouTube視頻,並在WordPress博客中向我顯示該視頻。博客搜索與可口可樂有關的新聞後,還會在帖子中顯示關於它的消息。可以動態呈現Wordpress帖子嗎?

FB連接,YouTube搜索或Google搜索都沒有問題。我的問題是WordPress。由於可能有很多用戶,並且可以爲每個用戶生成大量內容,因此我無法將每個帖子保存在MySQL表中。我想動態地生成帖子。我不是在這裏要求代碼,我只是想聽到很好的解決方案和想法,這怎麼能做到。

+0

你有沒有得到這個解決方案以任何方式工作?你試過什麼了? – ajtrichards

回答

3

作爲一個解決方案,您可以使用404頁面來生成這個動態帖子。

這裏有一個博客帖子,給出了一個類似的解決方案:http://www.blogseye.com/creating-fake-wordpress-posts-on-the-fly/

用來生成假的職位代碼:

function kpg_f_content() { 
    global $wp_query; 
    if($wp_query->is_404) { 
     $id=-42; // need an id 
     $post = new stdClass(); 
      $post->ID= $id; 
      $post->post_category= array('uncategorized'); //Add some categories. an array()??? 
      $post->post_content='hey here we are a real post'; //The full text of the post. 
      $post->post_excerpt= 'hey here we are a real post'; //For all your post excerpt needs. 
      $post->post_status='publish'; //Set the status of the new post. 
      $post->post_title= 'Fake Title'; //The title of your post. 
      $post->post_type='post'; //Sometimes you might want to post a page. 
     $wp_query->queried_object=$post; 
     $wp_query->post=$post; 
     $wp_query->found_posts = 1; 
     $wp_query->post_count = 1; 
     $wp_query->max_num_pages = 1; 
     $wp_query->is_single = 1; 
     $wp_query->is_404 = false; 
     $wp_query->is_posts_page = 1; 
     $wp_query->posts = array($post); 
     $wp_query->page=false; 
     $wp_query->is_post=true; 
     $wp_query->page=false; 
    } 
} 

add_action('wp', 'kpg_f_content'); 

將此成插件或將其添加到functions.php文件。

+1

謝謝你很好的解決方案。 –

0

替代解決方案:

add_filter('init', 'custom_function'); 
function custom_function(){ 
    if(is_admin()==true) return false; 

    //check URL 
    if($_SERVER['REQUEST_URI'] != '/hello/') return false; 

    echo 'Hello.'; 
    exit(); 
    } 
0

這是另一種方式來創造崗位動態

$post = array(
    'ID'    => [ <post id> ] // Are you updating an existing post? 
    'post_content' => [ <string> ] // The full text of the post. 
    'post_name'  => [ <string> ] // The name (slug) for your post 
    'post_title'  => [ <string> ] // The title of your post. 
    'post_status' => [ 'draft' | 'publish' | 'pending'| 'future' | 'private' | custom registered status ] // Default 'draft'. 
    'post_type'  => [ 'post' | 'page' | 'link' | 'nav_menu_item' | custom post type ] // Default 'post'. 
    'post_author' => [ <user ID> ] // The user ID number of the author. Default is the current user ID. 
    'ping_status' => [ 'closed' | 'open' ] // Pingbacks or trackbacks allowed. Default is the option 'default_ping_status'. 
    'post_parent' => [ <post ID> ] // Sets the parent of the new post, if any. Default 0. 
    'menu_order'  => [ <order> ] // If new post is a page, sets the order in which it should appear in supported menus. Default 0. 
    'to_ping'  => // Space or carriage return-separated list of URLs to ping. Default empty string. 
    'pinged'   => // Space or carriage return-separated list of URLs that have been pinged. Default empty string. 
    'post_password' => [ <string> ] // Password for post, if any. Default empty string. 
    'guid'   => // Skip this and let Wordpress handle it, usually. 
    'post_content_filtered' => // Skip this and let Wordpress handle it, usually. 
    'post_excerpt' => [ <string> ] // For all your post excerpt needs. 
    'post_date'  => [ Y-m-d H:i:s ] // The time post was made. 
    'post_date_gmt' => [ Y-m-d H:i:s ] // The time post was made, in GMT. 
    'comment_status' => [ 'closed' | 'open' ] // Default is the option 'default_comment_status', or 'closed'. 
    'post_category' => [ array(<category id>, ...) ] // Default empty. 
    'tags_input'  => [ '<tag>, <tag>, ...' | array ] // Default empty. 
    'tax_input'  => [ array(<taxonomy> => <array | string>) ] // For custom taxonomies. Default empty. 
    'page_template' => [ <string> ] // Requires name of template file, eg template.php. Default empty. 
); 
    $post_id = wp_insert_post($post); 

這幫助我很多!。

更多有關wp_insert_postclickhere