我创建了一个带有自定义日期、时间和复选框字段的自定义帖子类型。我对没有在WordPress管理或数据库中注册的日期和时间字段有问题。此外,一个自定义复选框字段也有问题,它会为我获取一个值,但如果我选中两个值,则只会显示一个值,并且不会在Wordpress管理员中注册。
我已附上我的代码:
class Fastcat_Metabox
{
private $id;
private $title;
private $post_type;
private $fields = [];
//Ajout uploader image parti organisateur
public static function addJS(){
add_action(\'admin_enqueue_scripts\', function(){
wp_register_script(\'uploaderjs\', get_template_directory_uri() . \'/assets/js/uplaoder.js\');
wp_enqueue_script(\'uploaderjs\');
});
}
/**
* Fastcat_Metabox constructor.
* @param $id ID de la boite
* @param $title Titre de la boite
* @param $post_type Post type
*/
// Construction de la function
public function __construct($id , $title, $post_type)
{
add_action(\'admin_init\', array(&$this, \'create\'));
add_action(\'save_post\', array(&$this, \'save\'));
$this->id = $id;
$this->title = $title;
$this->post_type = $post_type;
}
// Création de la fontion
public function create(){
if(function_exists(\'add_meta_box\')) {
add_meta_box($this->id , $this->title, array(&$this, \'render\'), $this->post_type);
}
//Sauvegarde des données et vérification des permission et du nonce
public function save($post_id){
//On ne fais rien en cas de save Ajax
if(
(defined(\'DOING_AUTOSAVE\') && DOING_AUTOSAVE) ||
(defined(\'DOING_AJAX\') && DOING_AJAX)
){
return false;
}
//Vérifier permission
if(!current_user_can(\'edit_post\', $post_id)){
return false;
}
//On vérifie le nonce
if (!wp_verify_nonce($_POST[$this->id . \'_nonce\'], $this->id)){
return false;
}
// Vérification de toutes les publications , suppression pour remplacement si déjà existant , ajout si nouveau etc
foreach($this->fields as $field) {
$meta = $field[\'id\'];
if(isset($_POST[$meta])){
$value = $_POST[$meta];
if(get_post_meta($post_id, $meta)){
update_post_meta($post_id, $meta, $value);
} else {
if ($value === \'\') {
delete_post_meta($post_id, $meta);
} else {
add_post_meta($post_id, $meta , $value );
}
}
}
}
}
// Affichage de tout les nouveaux champs ajouter dans le dashboard
public function render(){
global $post;
foreach($this->fields as $field) {
extract($field);
$value = get_post_meta($post->ID, $id , true);
if($value == \'\'){
$value = $default;
}
require __DIR__ .\'/\'. $field[\'type\'] . \'.php\';
}
echo \'<input type="hidden" name="\' .$this->id .\'_nonce" value="\' .wp_create_nonce($this->id). \'">\';
}
// Création des nouveaux champs
public function add($id, $label, $type = \'text\', $default=\'\') {
$this->fields[] = [
\'id\' => $id,
\'name\' => $label,
\'type\' => $type,
\'default\' => $default
];
return $this;
}
}
Fastcat_Metabox::addJS();
// CUSTOM POST TYPE -> COMPETITION -> Ajout des parties Information et Date supplémentaire
$box = new Fastcat_Metabox(\'compet\' , \'Compétition\', \'competition\');
$box->add(\'fastcat_adresse\',\'<strong> Adresse: </strong> \')
->add(\'fastcat_date\', \'<strong>Date </strong> \', \'date\')
->add(\'fastcat_starttime\', \'<strong>Heure de début </strong> \', \'time\')
->add(\'fastcat_endtime\', \' <strong>Heure de fin </strong>\', \'timeend\')
->add(\'fastcat_choice\', \'<strong>Choix compétitions </strong>\', \'checkbox\')
->add(\'fastcat_description\', \'<strong>Description de la compétition </strong>\', \'textarea\')
->add(\'fastcat_space\', \'<strong>Nombre de place </strong> \', \'number\')
->add(\'fastcat_rate\', \'<strong>Tarif </strong>\', \'rate\');
$box = new Fastcat_Metabox(\'ition\' , \'Date Supplémentaire\', \'competition\');
$box->add(\'fastcat_date2\', \'<strong>Date</strong> \', \'date2\')
->add(\'fastcat_starttime2\', \'<strong>Heure de début</strong> \', \'time2\')
->add(\'fastcat_endtime2\', \'<strong> Heure de fin </strong>\', \'timeend2\')
->add(\'fastcat_date3\', \'<strong>Date</strong> \', \'date3\')
->add(\'fastcat_starttime3\', \'<strong>Heure de début</strong> \', \'time3\')
->add(\'fastcat_endtime3\', \'<strong> Heure de fin</strong> \', \'timeend3\');
$box = new Fastcat_Metabox(\'cat\' , \'Organisateur\', \'competition\');
$box->add(\'fastcat_organisateur\',\'<strong>Nom de l\\\'organisateur</strong>\',\'name_organisateur\')
->add(\'fastcat_picture\', \'<strong Image de l\\\'organisateur</strong>\',\'picture_organisateur\')
->add(\'fastcat_club\', \'<strong>Numéro de club</strong> \', \'number_club\')
->add(\'fastcat_adresse\',\'<strong> Adresse: </strong> \', \'text\')
->add(\'fastcat_phone\', \'<strong> Téléphone </strong>\', \'phone\')
->add(\'fastcat_mail\', \'<strong>Email</strong> \', \'mail\')
->add(\'fastcat_siteweb\', \'<strong>Site internet</strong> \', \'web\')
->add(\'fastcat_description_organisateur\', \'<strong>Description de la compétition </strong>\', \'description\');
//CHECKBOX
<div class="meta-box-item-title" style="padding-bottom:0.3rem; ">
<?= $name; ?>
</div>
<div class="meta-box-item-content" style="padding-bottom:0.3rem; ">
<input type="checkbox" id="<?= $id; ?>" name="<?= $id; ?>" value="Cat">
<label for="<?= $value; ?>" style="padding-right:1rem;">CAT</label>
<input type="checkbox" id="<?= $id; ?>" name="<?= $id; ?>" value="FastCat">
<label for="<?= $value; ?>">FASTCAT</label>
</div>
//DATE
<div class="meta-box-item-title" style="padding-bottom:0.3rem; ">
<?= $name; ?>
</div>
<div class="meta-box-item-content" style="padding-bottom:0.3rem; ">
<input type="text" name="<?= $id; ?>" id="<?= $id; ?>" value="<?= $value; ?>">
</div>
//TIME
<div class="meta-box-item-title" style="padding-bottom:0.3rem; ">
<?= $name; ?>
</div>
<div class="meta-box-item-content" style="padding-bottom:0.3rem; ">
<input type="text" name="<?= $id; ?>" id="<?= $id; ?>" value="<?= $value; ?>">
</div>
你能帮帮我吗?