我正在构建一个函数,用于创建一组具有公共父ID的页面。
如果我对父ID运行两次wp\\u insert\\u post(),函数是否会在更改段塞的同时创建两次页面?或者我可以把它放在我的功能中吗。php类似:
add_action(\'init\', \'AddMyPages\');
我正在构建一个函数,用于创建一组具有公共父ID的页面。
如果我对父ID运行两次wp\\u insert\\u post(),函数是否会在更改段塞的同时创建两次页面?或者我可以把它放在我的功能中吗。php类似:
add_action(\'init\', \'AddMyPages\');
使用某种类型的条件标记来检查这些帖子是否存在。如果它们不存在,请使用wp_insert_post
. 在AddMyPages
功能,而不是围绕add_action
作用
Example
您希望仅在父ID不存在时添加该页,并希望该页始终存在。因为它在init
钩子,它将始终检查该页面是否存在,因此不建议这样做。你应该这样做on activation 的plugin (所以我编辑了该方法的答案)。register_activation_hook( __FILE__, \'AddThisPage\' );
function AddThisPage() {
global $wpdb; // Not sure if you need this, maybe
$page = array(
\'post_title\' => \'My post\',
\'post_content\' => \'This is my post.\',
\'post_status\' => \'publish\',
\'post_author\' => 1,
\'post_type\' => \'page\',
\'post_parent\' => 3 // ID of the parent page
);
$page_exists = get_page_by_title( $page[\'post_title\'] );
if( $page_exists == null ) {
// Page doesn\'t exist, so lets add it
$insert = wp_insert_post( $page );
if( $insert ) {
// Page was inserted ($insert = new page\'s ID)
}
} else {
// Page already exists
}
}
感谢@kaiser提醒我,register_activation_hook
仅在中运行plugins, 不是主题。至于主题,我不知道官方的激活挂钩,只是switch_theme
但这是在主题激活之前运行的。我发现a workaround here 但它可能已经过时,可能是一些有用的研究。
Resources used
wp_insert_post, get_page_by_title, register_activation_hook我偶然发现this question 在写这个问题的时候。我有一个问题是关于这个问题的。我发现你用的是get_delete_post_link 筛选为我的操作创建一个新的url(或一个类似的函数——在任何情况下,我都会将该函数与布尔值一起使用)。唯一的问题是,I don\'t know how to capture the event now. 考虑到我在谷歌上找不到很多关于行后操作的例子,我将不胜感激-/public function _wp_filter_get_delete_post_link( $