_reset_front_page_settings_for_post()
云策文档标注
概述
_reset_front_page_settings_for_post() 函数用于在关联页面被删除或移至回收站时,重置 WordPress 的前端页面设置。它会检查并更新 page_on_front、show_on_front 和 page_for_posts 选项,同时确保帖子不再置顶。
关键要点
- 当页面类型为 'page' 时,函数会检查该页面是否被设置为首页(page_on_front)或文章页(page_for_posts),并相应重置选项值。
- 如果页面是首页,则更新 show_on_front 为 'posts' 并将 page_on_front 设为 0。
- 如果页面是文章页,则将 page_for_posts 设为 0。
- 无论页面类型如何,都会调用 unstick_post() 函数来移除帖子的置顶状态。
- 函数自 WordPress 3.7.0 版本引入,适用于处理页面删除或移至回收站时的设置清理。
代码示例
function _reset_front_page_settings_for_post( $post_id ) {
$post = get_post( $post_id );
if ( 'page' === $post->post_type ) {
/*
* If the page is defined in option page_on_front or post_for_posts,
* adjust the corresponding options.
*/
if ( (int) get_option( 'page_on_front' ) === $post->ID ) {
update_option( 'show_on_front', 'posts' );
update_option( 'page_on_front', 0 );
}
if ( (int) get_option( 'page_for_posts' ) === $post->ID ) {
update_option( 'page_for_posts', 0 );
}
}
unstick_post( $post->ID );
}
原文内容
Resets the page_on_front, show_on_front, and page_for_post settings when a linked page is deleted or trashed.
Description
Also ensures the post is no longer sticky.
Parameters
$post_idintrequired-
Post ID.
Source
function _reset_front_page_settings_for_post( $post_id ) {
$post = get_post( $post_id );
if ( 'page' === $post->post_type ) {
/*
* If the page is defined in option page_on_front or post_for_posts,
* adjust the corresponding options.
*/
if ( (int) get_option( 'page_on_front' ) === $post->ID ) {
update_option( 'show_on_front', 'posts' );
update_option( 'page_on_front', 0 );
}
if ( (int) get_option( 'page_for_posts' ) === $post->ID ) {
update_option( 'page_for_posts', 0 );
}
}
unstick_post( $post->ID );
}
Changelog
| Version | Description |
|---|---|
| 3.7.0 | Introduced. |