【问题标题】:Can Wordpress posts be rendered dynamically?Wordpress 帖子可以动态呈现吗?
【发布时间】:2014-01-08 04:57:00
【问题描述】:

我想创建一个博客页面,该页面根据特定用户的 Facebook 喜欢、活动等生成内容。例如,我喜欢 Facebook 上的 Shakira 和 Coca Cola。当进入博客并通过 Facebook 连接时,博客会获取该信息并通过 YouTube API 搜索夏奇拉的 YouTube 视频,并在 WordPress 帖子中显示该视频。在博客搜索与可口可乐相关的新闻并在帖子中显示有关它的新闻之后。

FB 连接、YouTube 搜索或 Google 搜索没有问题。我的问题是 WordPress。由于可以有很多用户并且可以为每个用户生成很多内容,我无法将每个帖子都保存在 MySQL 表中。我想动态生成帖子。我不是在这里要求代码,我只是想听听好的解决方案和想法如何做到这一点。

【问题讨论】:

  • 你有没有让这个解决方案以任何方式工作?到目前为止,您尝试过什么?

标签: php facebook wordpress wordpress-theming


【解决方案1】:

作为一种解决方案,您可以使用 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文件中。

【讨论】:

    【解决方案2】:

    替代解决方案:

    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();
        }
    

    【讨论】:

      【解决方案3】:

      这是另一种动态创建帖子的方式

          $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的信息

      【讨论】:

        猜你喜欢
        • 2013-08-03
        • 1970-01-01
        • 1970-01-01
        • 2012-03-14
        • 2014-02-23
        • 2023-03-26
        • 2010-11-24
        • 2011-03-14
        • 1970-01-01
        相关资源
        最近更新 更多