【问题标题】:Automatically create page in WordPress在 WordPress 中自动创建页面
【发布时间】:2010-11-14 05:39:00
【问题描述】:

如何自动创建 WordPress 页面(例如,激活插件时)?

【问题讨论】:

  • 你到底是什么意思?你想编写一个使用 wp 核心 API 创建页面的插件吗?
  • 是的,就是这样。我想创建新页面,而不是在某处插入现有页面。

标签: wordpress


【解决方案1】:

使用wp_insert_post(),也可以插入页面:http://codex.wordpress.org/Function_Reference/wp_insert_post

请参阅下面的 post_type。

$post = array(
  'ID' => [ <post id> ] //Are you updating an existing post?
  'menu_order' => [ <order> ] //If new post is a page, sets the order should it appear in the tabs.
  'page_template' => [ <template file> ] //Sets the template for the page.
  'comment_status' => [ 'closed' | 'open' ] // 'closed' means no comments.
  'ping_status' => [ ? ] //Ping status?
  'pinged' => [ ? ] //?
  'post_author' => [ <user ID> ] //The user ID number of the author.
  'post_category' => [ array(<category id>, <...>) ] //Add some categories.
  'post_content' => [ <the text of the post> ] //The full text of the post.
  '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.
  'post_excerpt' => [ <an excerpt> ] //For all your post excerpt needs.
  'post_name' => [ <the name> ] // The name (slug) for your post
  'post_parent' => [ <post ID> ] //Sets the parent of the new post.
  'post_password' => [ ? ] //password for post?
  'post_status' => [ 'draft' | 'publish' | 'pending' ] //Set the status of the new post.
  'post_title' => [ <the title> ] //The title of your post.
  'post_type' => [ 'post' | 'page' ] //Sometimes you want to post a page.
  'tags_input' => [ '<tag>, <tag>, <...>' ] //For tags.
  'to_ping' => [ ? ] //?
);  

// Insert the post into the database
wp_insert_post( $post );

【讨论】:

  • 因为页面只是被标记为页面的帖子。
  • 谢谢。我认为更容易:)
  • 另外,新手插件开发问题...当我激活插件时这会创建页面还是我需要添加一些代码来指定我希望插件在激活时创建该页面?跨度>
  • 这取决于你。当你制作插件时,当插件被激活时,你有空间做事情,还有大量的钩子和动作,你可以挂钩并只在某个事件发生时做事情。在线阅读 WP 插件开发。
  • 当我将它添加到我的functions.php时,此代码进入无限循环
【解决方案2】:

Wordpress 为数据库抽象提供了 wp->query API 方法。您可以在需要时创建适当的查询来创建页面。

【讨论】:

  • 总的来说,这是一个非常糟糕的建议。仅当您无法使用 API 函数实现相同功能时,才应使用查询。主要原因是未来的表更改可能会破坏您的查询,而函数希望得到维护。
猜你喜欢
  • 1970-01-01
  • 2016-12-11
  • 2015-01-25
  • 1970-01-01
  • 2016-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多