我正在为一个本地联盟开发一个非常简单的自定义插件,他需要一些东西来管理球队和球员以及他们的积分和固定装置。 我计划使用post_type,meta_box和taxonomy函数来开发插件。 我不是专家,但我知道这些功能的基本用法。我已经陷入了需要在后端显示多个输入来填补团队的地步。站立属性;例如,赢,抽,亏,分。我能够在管理面板中显示字段,但我只知道如何保存一个输入字段数据,但由于有几个数据,我尝试了几乎所有我知道的方法和很多网站,但是没有'让他们得救。请查看代码,
我可以通过这些代码成功保存一个输入字段数据
<?php
/*
Plugin Name: Manage Clubs
Plugin URI: https://www.odesk.com/users/~01a9f6045443021f0c
Description: A simple lightweight plugin to manage your legue.
Author: Abubakkar Roby
Author URI: https://www.odesk.com/users/~01a9f6045443021f0c
Version: 1.0
*/
class RN_Manage_Clubs {
public function __construct() {
$this->register_post_type();
$this->statistics();
}
public function register_post_type() {
$args = array(
'labels' => array(
'name' => 'Manage Clubs',
'all_items' => 'Clubs',
'singular_name' => 'Club',
'add_new' => 'Add New Club',
'add_new_item' => 'Add New Club',
'edit_item' => 'Edit Club',
'new_item' => 'Add New Item',
'view_item' => 'View Club',
'search_items' => 'Search Clubs',
'not_found' => 'No Clubs Found',
'not_found_in_trash' => 'No Clubs Found In Trash'
),
'query_var' => 'Clubs',
'rewrite' => array( 'slug' => 'clubs/'),
//'menu_position' => 5,
'menu_icon' => admin_url() . 'images/icon_soccer.png',
'supports' => ['title', 'thumbnail'],
'public' => true
);
register_post_type('RN_Manage_Clubs', $args);
}
public function statistics() {
add_action('add_meta_boxes', function() {
//css id, title, cb func, assoc. page/post, priority, cb func args
add_meta_box('rn_club_stat', 'Club Standing', 'club_stats', 'RN_Manage_Clubs');
});
function club_stats($post) {
$POS = get_post_meta($post->ID, 'rn_club_stat', true);
?>
<input type="text" name="rn_club_stat" id="rn_club_stat" value="<?php echo esc_attr($POS); ?>" size="1" tabindex="-1" />
<?php
}
add_action('save_post', function($id) {
if( isset($_POST['rn_club_stat']) ) {
update_post_meta(
$id,
'rn_club_stat',
strip_tags($_POST['rn_club_stat'])
);
}
});
}
}
add_action('init', function() {
new RN_Manage_Clubs();
});
但是我如何保存来自多个输入字段的数据,例如这些字段
<?php
/*
Plugin Name: Manage Clubs
Plugin URI: https://www.odesk.com/users/~01a9f6045443021f0c
Description: A simple lightweight plugin to manage your legue.
Author: Abubakkar Roby
Author URI: https://www.odesk.com/users/~01a9f6045443021f0c
Version: 1.0
*/
class RN_Manage_Clubs {
public function __construct() {
$this->register_post_type();
$this->statistics();
}
public function register_post_type() {
$args = array(
'labels' => array(
'name' => 'Manage Clubs',
'all_items' => 'Clubs',
'singular_name' => 'Club',
'add_new' => 'Add New Club',
'add_new_item' => 'Add New Club',
'edit_item' => 'Edit Club',
'new_item' => 'Add New Item',
'view_item' => 'View Club',
'search_items' => 'Search Clubs',
'not_found' => 'No Clubs Found',
'not_found_in_trash' => 'No Clubs Found In Trash'
),
'query_var' => 'Clubs',
'rewrite' => array( 'slug' => 'clubs/'),
//'menu_position' => 5,
'menu_icon' => admin_url() . 'images/icon_soccer.png',
'supports' => ['title', 'thumbnail'],
'public' => true
);
register_post_type('RN_Manage_Clubs', $args);
}
public function statistics() {
add_action('add_meta_boxes', function() {
//css id, title, cb func, assoc. page/post, priority, cb func args
add_meta_box('rn_club_stat', 'Club Standing', 'club_stats', 'RN_Manage_Clubs');
});
function club_stats($post) {
$POS = get_post_meta($post->ID, 'rn_club_stat', true);
?>
<table>
<thead>
<tr>
<td> </td>
<th>POS</th>
<th>W</th>
<th>D</th>
<th>L</th>
<th>F</th>
<th>A</th>
<th>GD</th>
<th>PTS</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Total</th>
<td><input type="text" name="POS" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="W" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="D" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="L" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="F" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="A" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="GD" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="PTS" value="" size="1" tabindex="-1" readonly /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Values</td>
<td><input type="text" name="POS" id="POS" value="" size="1" tabindex="-1" /></td>
<td><input type="text" name="W" value="" size="1" tabindex="-1" /></td>
<td><input type="text" name="D" value="" size="1" tabindex="-1" /></td>
<td><input type="text" name="L" value="" size="1" tabindex="-1" /></td>
<td><input type="text" name="F" value="" size="1" tabindex="-1" /></td>
<td><input type="text" name="A" value="" size="1" tabindex="-1" /></td>
<td><input type="text" name="GD" value="" size="1" tabindex="-1" /></td>
<td><input type="text" name="PTS" value="" size="1" tabindex="-1" /></td>
</tr>
</tbody>
</table>
<?php
}
add_action('save_post', function($id) {
if( isset($_POST['rn_club_stat']) ) {
update_post_meta(
$id,
'rn_club_stat',
strip_tags($_POST['rn_club_stat'])
);
}
});
}
}
add_action('init', function() {
new RN_Manage_Clubs();
});
@gorirrajoe,我已经使用get_post_custom()函数找到了解决方案。但我也会尝试你的代码。 这是为我做这份工作的片段
<?php
/*
Plugin Name: Manage League
Plugin URI: https://www.odesk.com/users/~01a9f6045443021f0c
Description: A simple lightweight plugin to manage your legue.
Author: Abubakkar Roby
Author URI: https://www.odesk.com/users/~01a9f6045443021f0c
Version: 1.0
*/
class RN_Manage_Clubs {
public function __construct() {
$this->register_post_type();
$this->statistics();
}
public function register_post_type() {
$args = array(
'labels' => array(
'name' => 'Manage Clubs',
'all_items' => 'Clubs',
'singular_name' => 'Club',
'add_new' => 'Add New Club',
'add_new_item' => 'Add New Club',
'edit_item' => 'Edit Club',
'new_item' => 'Add New Item',
'view_item' => 'View Club',
'search_items' => 'Search Clubs',
'not_found' => 'No Clubs Found',
'not_found_in_trash' => 'No Clubs Found In Trash'
),
'query_var' => 'Clubs',
'rewrite' => array( 'slug' => 'clubs/'),
//'menu_position' => 5,
'menu_icon' => admin_url() . 'images/icon_soccer.png',
'supports' => ['title', 'editor', 'thumbnail'],
'public' => true
);
register_post_type('RN_Manage_Clubs', $args);
}
public function statistics() {
add_action('add_meta_boxes', function() {
//css id, title, cb func, assoc. page/post, priority, cb func args
add_meta_box('rn_club_stat', 'Club Standing', 'club_stats', 'RN_Manage_Clubs');
});
function club_stats($post) {
//$value = get_post_meta($post->ID, 'rn_club_stat', false);
global $post;
$custom = get_post_custom($post->ID);
$POS = $custom['POS'][0];
$P = $custom['P'][0];
$W = $custom['W'][0];
$D = $custom['D'][0];
$L = $custom['L'][0];
$B = $custom['B'][0];
$PTS = $custom['PTS'][0];
?>
<table>
<thead>
<tr>
<td> </td>
<th>POS</th>
<th>P</th>
<th>W</th>
<th>D</th>
<th>L</th>
<th>B</th>
<th>PTS</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Total</th>
<td><input type="text" name="POS" id="POS" value="<?php echo $POS; ?>" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="P" id="P" value="<?php echo $P; ?>" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="W" value="<?php echo $W; ?>" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="D" value="<?php echo $D; ?>" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="L" value="<?php echo $L; ?>" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="F" value="<?php echo $B; ?>" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="PTS" value="<?php echo $PTS; ?>" size="1" tabindex="-1" readonly /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Values</td>
<td><input type="text" name="POS" id="POS" value="<?php echo $POS; ?>" size="1" tabindex="-1" /></td>
<td><input type="text" name="P" id="P" value="<?php echo $P; ?>" size="1" tabindex="-1" /></td>
<td><input type="text" name="W" id="W" value="<?php echo $W; ?>" size="1" tabindex="-1" /></td>
<td><input type="text" name="D" value="<?php echo $D; ?>" size="1" tabindex="-1" /></td>
<td><input type="text" name="L" value="<?php echo $L; ?>" size="1" tabindex="-1" /></td>
<td><input type="text" name="B" value="<?php echo $B; ?>" size="1" tabindex="-1" /></td>
<td><input type="text" name="PTS" value="<?php echo $PTS; ?>" size="1" tabindex="-1" /></td>
</tr>
</tbody>
</table>
<?php
}
add_action('save_post', function($id) {
if( isset($_POST['POS']) ) {
update_post_meta($id,'POS', $_POST['POS']);
}
if( isset($_POST['P']) ) {
update_post_meta($id,'P', $_POST['P']);
}
if( isset($_POST['W']) ) {
update_post_meta($id,'W', $_POST['W']);
}
if( isset($_POST['D']) ) {
update_post_meta($id,'D', $_POST['D']);
}
if( isset($_POST['L']) ) {
update_post_meta($id,'L', $_POST['L']);
}
if( isset($_POST['B']) ) {
update_post_meta($id,'B', $_POST['B']);
}
if( isset($_POST['PTS']) ) {
update_post_meta($id,'PTS', $_POST['PTS']);
}
});
}
}
add_action('init', function() {
new RN_Manage_Clubs();
});
答案 0 :(得分:0)
您的代码只是想查看是否设置了“rn_club_stat”。您需要检查所有字段。
<?php
/*
Plugin Name: Manage Clubs
Plugin URI: https://www.odesk.com/users/~01a9f6045443021f0c
Description: A simple lightweight plugin to manage your legue.
Author: Abubakkar Roby
Author URI: https://www.odesk.com/users/~01a9f6045443021f0c
Version: 1.0
*/
class RN_Manage_Clubs {
public function __construct() {
$this->register_post_type();
$this->statistics();
}
public function register_post_type() {
$args = array(
'labels' => array(
'name' => 'Manage Clubs',
'all_items' => 'Clubs',
'singular_name' => 'Club',
'add_new' => 'Add New Club',
'add_new_item' => 'Add New Club',
'edit_item' => 'Edit Club',
'new_item' => 'Add New Item',
'view_item' => 'View Club',
'search_items' => 'Search Clubs',
'not_found' => 'No Clubs Found',
'not_found_in_trash' => 'No Clubs Found In Trash'
),
'query_var' => 'Clubs',
'rewrite' => array( 'slug' => 'clubs/'),
//'menu_position' => 5,
'menu_icon' => admin_url() . 'images/icon_soccer.png',
'supports' => ['title', 'thumbnail'],
'public' => true
);
register_post_type('RN_Manage_Clubs', $args);
}
public function statistics() {
add_action('add_meta_boxes', function() {
//css id, title, cb func, assoc. page/post, priority, cb func args
add_meta_box('rn_club_stat', 'Club Standing', 'club_stats', 'RN_Manage_Clubs');
});
function club_stats($post) {
?>
<table>
<thead>
<tr>
<td> </td>
<th>POS</th>
<th>W</th>
<th>D</th>
<th>L</th>
<th>F</th>
<th>A</th>
<th>GD</th>
<th>PTS</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Total</th>
<td><input type="text" name="POS_total" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="W_total" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="D_total" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="L_total" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="F_total" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="A_total" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="GD_total" value="" size="1" tabindex="-1" readonly /></td>
<td><input type="text" name="PTS_total" value="" size="1" tabindex="-1" readonly /></td>
</tr>
</tfoot>
<tbody>
<tr>
<td>Values</td>
<td><input type="text" name="pos" id="pos" value="<?php echo get_post_meta($post->ID, '_pos', true); ?>" size="1" tabindex="1" /></td>
<td><input type="text" name="w" value="<?php echo get_post_meta($post->ID, '_w', true); ?>" size="1" tabindex="2" /></td>
<td><input type="text" name="d" value="<?php echo get_post_meta($post->ID, '_d', true); ?>" size="1" tabindex="3" /></td>
<td><input type="text" name="l" value="<?php echo get_post_meta($post->ID, '_l', true); ?>" size="1" tabindex="4" /></td>
<td><input type="text" name="f" value="<?php echo get_post_meta($post->ID, '_f', true); ?>" size="1" tabindex="5" /></td>
<td><input type="text" name="a" value="<?php echo get_post_meta($post->ID, '_a', true); ?>" size="1" tabindex="6" /></td>
<td><input type="text" name="gd" value="<?php echo get_post_meta($post->ID, '_gd', true); ?>" size="1" tabindex="7" /></td>
<td><input type="text" name="pts" value="<?php echo get_post_meta($post->ID, '_pts', true); ?>" size="1" tabindex="8" /></td>
</tr>
</tbody>
</table>
<?php
}
add_action('save_post', function($id) {
$new_meta_value = ( isset( $_POST['pos'] ) ? $_POST['pos'] : '' );
$meta_value = get_post_meta( $id, '_pos', true );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $id, '_pos', $new_meta_value, true );
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $id, '_pos', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $id, '_pos', $meta_value );
$new_meta_value = ( isset( $_POST['w'] ) ? $_POST['w'] : '' );
$meta_value = get_post_meta( $id, '_w', true );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $id, '_w', $new_meta_value, true );
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $id, '_w', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $id, '_w', $meta_value );
$new_meta_value = ( isset( $_POST['d'] ) ? $_POST['d'] : '' );
$meta_value = get_post_meta( $id, '_d', true );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $id, '_d', $new_meta_value, true );
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $id, '_d', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $id, '_d', $meta_value );
$new_meta_value = ( isset( $_POST['l'] ) ? $_POST['l'] : '' );
$meta_value = get_post_meta( $id, '_l', true );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $id, '_l', $new_meta_value, true );
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $id, '_l', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $id, '_l', $meta_value );
$new_meta_value = ( isset( $_POST['f'] ) ? $_POST['f'] : '' );
$meta_value = get_post_meta( $id, '_f', true );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $id, '_f', $new_meta_value, true );
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $id, '_f', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $id, '_f', $meta_value );
$new_meta_value = ( isset( $_POST['a'] ) ? $_POST['a'] : '' );
$meta_value = get_post_meta( $id, '_a', true );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $id, '_a', $new_meta_value, true );
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $id, '_a', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $id, '_a', $meta_value );
$new_meta_value = ( isset( $_POST['gd'] ) ? $_POST['gd'] : '' );
$meta_value = get_post_meta( $id, '_gd', true );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $id, '_gd', $new_meta_value, true );
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $id, '_gd', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $id, '_gd', $meta_value );
$new_meta_value = ( isset( $_POST['pts'] ) ? $_POST['pts'] : '' );
$meta_value = get_post_meta( $id, '_pts', true );
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $id, '_pts', $new_meta_value, true );
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $id, '_pts', $new_meta_value );
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $id, '_pts', $meta_value );
});
}
}
add_action('init', function() {
new RN_Manage_Clubs();
});