我想在添加/编辑帖子页面的Publish块中添加一个新的复选框字段。有谁知道怎么做?
答案 0 :(得分:20)
我终于找到了解决方案。我希望它对某人有用。
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']);
}
答案 1 :(得分:12)
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
答案 2 :(得分:2)
嗯!,我找不到在发布块中添加字段的解决方案。对于临时解决方案,我通过简单地添加如下的简单代码添加了新块。
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']);
}
//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']);
}
答案 3 :(得分:-2)
使用Advanced Custom Fields插件进行wordpress。