[英]How to add a field in edit post page inside Publish box in Wordpress?
我想在添加/編輯帖子頁面的Publish塊中添加一個新的復選框字段。 有誰知道怎么做?
我終於找到了解決方案。 我希望它對某人有用。
add_action( 'post_submitbox_misc_actions', 'publish_in_frontpage' );
function publish_in_frontpage($post)
{
$value = get_post_meta($post->ID, '_publish_in_frontpage', true);
echo '<div class="misc-pub-section misc-pub-section-last">
<span id="timestamp">'
. '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>'
.'</span></div>';
}
function save_postdata($postid)
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
if ( !current_user_can( 'edit_page', $postid ) ) return false;
if(empty($postid) || $_POST['post_type'] != 'article' ) return false;
if($_POST['action'] == 'editpost'){
delete_post_meta($postid, 'publish_in_frontpage');
}
add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']);
}
rbncha的代碼沒有開箱即用,需要進行大量的調整,下面的代碼就是我想出來的。 我添加了一些評論,徹底解釋了一切。
以下代碼在帖子的發布塊中添加了一個復選框(您可以輕松更改帖子類型),並在數據庫中存儲/檢索值。 通過一些小調整,您可以輕松添加文本字段或任何您喜歡的內容。
應該注意的是,您必須將my_
更改為主題或插件的唯一鍵 !
add_action( 'post_submitbox_misc_actions', 'my_featured_post_field' );
function my_featured_post_field()
{
global $post;
/* check if this is a post, if not then we won't add the custom field */
/* change this post type to any type you want to add the custom field to */
if (get_post_type($post) != 'post') return false;
/* get the value corrent value of the custom field */
$value = get_post_meta($post->ID, 'my_featured_post_field', true);
?>
<div class="misc-pub-section">
<?php //if there is a value (1), check the checkbox ?>
<label><input type="checkbox"<?php echo (!empty($value) ? ' checked="checked"' : null) ?> value="1" name="my_featured_post_field" /> Featured on frontpage</label>
</div>
<?php
}
add_action( 'save_post', 'my_save_postdata');
function my_save_postdata($postid)
{
/* check if this is an autosave */
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;
/* check if the user can edit this page */
if ( !current_user_can( 'edit_page', $postid ) ) return false;
/* check if there's a post id and check if this is a post */
/* make sure this is the same post type as above */
if(empty($postid) || $_POST['post_type'] != 'post' ) return false;
/* if you are going to use text fields, then you should change the part below */
/* use add_post_meta, update_post_meta and delete_post_meta, to control the stored value */
/* check if the custom field is submitted (checkboxes that aren't marked, aren't submitted) */
if(isset($_POST['my_featured_post_field'])){
/* store the value in the database */
add_post_meta($postid, 'my_featured_post_field', 1, true );
}
else{
/* not marked? delete the value in the database */
delete_post_meta($postid, 'my_featured_post_field');
}
}
如果您想了解有關自定義字段的更多信息,請參閱此處: http : //codex.wordpress.org/Custom_Fields
好吧!,我找不到在Publish Block中添加字段的解決方案。 對於臨時解決方案,我通過簡單地添加如下的簡單代碼添加了新塊。
add_action( 'admin_init', 'category_metabox');
//add new publish to frontpage box add_meta_box( 'publish_in_frontpage', 'Publish in Frontpage', 'publish_in_frontpage_callback', 'article', 'side', 'high' ); function publish_in_frontpage_callback($post) { $value = get_post_meta($post->ID, '_publish_in_frontpage', true); echo '<label><input type="checkbox"' . (!empty($value) ? ' checked="checked" ' : null) . 'value="1" name="publish_in_frontpage" /> Publish to frontpage</label>'; } add_action( 'save_post', 'save_postdata'); function save_postdata($postid) { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false; if ( !current_user_can( 'edit_page', $postid ) ) return false; if(empty($postid) || $_POST['post_type'] != 'article' ) return false; if($_POST['action'] == 'editpost'){ delete_post_meta($postid, 'publish_in_frontpage'); } add_post_meta($postid, 'publish_in_frontpage', $_POST['publish_in_frontpage']); }
使用WordPress的高級自定義字段插件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.