我想在Page Attribute
您在页面管理界面右侧看到的框。
有人知道这是否可行吗?
或者至少在此框下方添加一个自定义框?
我想在Page Attribute
您在页面管理界面右侧看到的框。
有人知道这是否可行吗?
或者至少在此框下方添加一个自定义框?
没有方便的挂钩可以往那个盒子里放东西。
你可以做两件事中的一件。
1. Add a new Meta Box
你可以通过钩住add_meta_boxes
操作和调用add_meta_box
. 您可以在调用add\\u meta\\u框中指定回调函数。该回调将负责回显您的选择列表。<?php
add_action( \'add_meta_boxes\', \'wpse44966_add_meta_box\' );
/**
* Adds the meta box to the page screen
*/
function wpse44966_add_meta_box()
{
add_meta_box(
\'wpse44966-meta-box\', // id, used as the html id att
__( \'WPSE 44966 Meta Box\' ), // meta box title, like "Page Attributes"
\'wpse44966_meta_box_cb\', // callback function, spits out the content
\'page\', // post type or page. We\'ll add this to pages only
\'side\', // context (where on the screen
\'low\' // priority, where should this go in the context?
);
}
/**
* Callback function for our meta box. Echos out the content
*/
function wpse44966_meta_box_cb( $post )
{
// create your dropdown here
}
2. Remove the Default Page attributes meta box, add your own version
除主编辑器和标题区域外,后期编辑屏幕上的所有内容都是元框。您可以通过调用remove_meta_box
, 然后用自己的替换它们。因此,首先,修改上面的add函数以包含remove meta box调用。那么你需要复制page_attributes_meta_box
功能主体来自wp-admin/includes/meta-boxes.php
把你的东西放在下面。
<?php
add_action( \'add_meta_boxes\', \'wpse44966_add_meta_box\' );
/**
* Adds the meta box to the page screen
*/
function wpse44966_add_meta_box( $post_type )
{
// remove the default
remove_meta_box(
\'pageparentdiv\',
\'page\',
\'side\'
);
// add our own
add_meta_box(
\'wpse44966-meta-box\',
\'page\' == $post_type ? __(\'Page Attributes\') : __(\'Attributes\'),
\'wpse44966_meta_box_cb\',
\'page\',
\'side\',
\'low\'
);
}
/**
* Callback function for our meta box. Echos out the content
*/
function wpse44966_meta_box_cb( $post )
{
// Copy the the `page_attributes_meta_box` function content here
// add your drop down
}
无论哪种方式,你都需要save_post
将字段值保存为add_post_meta
和/或update_post_meta
.<?php
add_action( \'save_post\', \'wpse44966_save_post\' );
/**
* Save our custom field value
*/
function wpse44966_save_post( $post_id )
{
// check nonces, permissions here
// save the data with update_post_meta
}
This tutorial 可能会帮助你。呜呜!从WP 4.9.0开始,有一个操作挂钩可以向页面属性元框添加代码:\'page_attributes_misc_attributes\'
. 看到了吗Codex for reference.
示例:
function wpse44966_page_attributes( $post ) {
?>
<p class="post-attributes-label-wrapper wpse44966-label-wrapper">
<label class="wpse44966-label" for="wpse44966_option" style="font-weight: 600;"><?php _e( \'Option Label\', \'textdomain\' ); ?></label>
<input id="wpse44966_option" name="wpse44966_option" type="checkbox" value="1" style="margin-left: 5px;"<?php if( isset( $post->wpse44966_option ) && $post->wpse44966_option ) echo \' checked="checked"\'; ?>>
</p>
<?php
}
add_action( \'page_attributes_misc_attributes\', \'wpse44966_page_attributes\' );
function wpse44966_metabox_save_post( $post_id ) {
if( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE ) return;
if( ! isset( $_POST[\'wpse44966_page_attributes_nonce\'] ) || ! wp_verify_nonce( $_POST[\'wpse44966_page_attributes_nonce\'], \'wpse44966_page_attributes_nonce\' ) ) return;
if( ! current_user_can( \'edit_post\' ) ) return;
$wpse44966_option = (int) ( isset( $_POST[\'wpse44966_option\'] ) && $_POST[\'wpse44966_option\'] );
update_post_meta( $post_id, \'wpse44966_option\', $wpse44966_option );
}
add_action( \'save_post\', \'wpse44966_metabox_save_post\' );