我有一个“活动”自定义帖子类型,其中包含特定原因的详细信息(一段左右)。
同一帖子上有一张用于支付处理的“捐赠”表单(用于发布用户详细信息、捐赠金额等),该表单从前端更新捐赠自定义帖子类型fmeta。
然而,要更新捐款贴子元,它需要获得捐款贴子类型的global$post\\u id,还需要选择活动贴子类型。
以下是表格代码:
<?php
if (isset( $_POST[‘cpt_nonce_field’] ) && wp_verify_nonce( $_POST[‘cpt_nonce_field’],‘cpt_nonce_action’ ) && ($post_id) )
{
update_post_meta($post_id, donations_metabox_donation-id, $_POST[\'donation_id\']);
update_post_meta($post_id, donations_metabox_cause-id, $_POST[\'cause_id\']);
update_post_meta($post_id, cause-title, $_POST[\'donation_for\']);
update_post_meta($post_id, donations_metabox_donation-currency, $_POST[\'donation_currency\']);
update_post_meta($post_id, donations_metabox_cause-amount, $_POST[\'cause_amount\']);
update_post_meta($post_id, donations_metabox_full-name, $_POST[\'full_name\']);
update_post_meta($post_id, donations_metabox_email, $_POST[\'email\']);
}
?>
<?php get_header(); ?>
<!-- main_wrapper -->
<div id="main_wrapper">
<div class="content-wrapper">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- tbl-layout -->
<div class="tbl-layout">
<div class="col left">
<h1><?php the_title(); ?></h1>
<?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>
<p><?php the_content(); ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
<div class="col right">
<h2>Your Donation</h2>
<!-- donation start -->
<form method="post" action="">
<label for="donation_for">
I would like to donate to
</label><br>
<select name="donation_for" id="donation_for" class="dd">
<?php
$args = array( \'post_type\'=>\'cause\',\'posts_per_page\' => -1,);
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
?>
<option value="<?php the_title(); ?>"><?php the_title(); ?></option>
<?php endforeach; ?>
</select>
<br>
<label for="donation_currency">Currency</label><br>
<select name="donation_currency[]" id="donation_currency" class="dd">
<option value="USD">USD</option>
<option value="KES">KES</option>
<option value="ZAR">ZAR</option>
</select>
<br><br>
<label for="cause_amount">Donation Amount</label><br>
<input type="text" class="textbox" id="cause_amount" name="cause_amount" placeholder="Enter Amount" maxlength="100" autocomplete="off" />
<br>
<br>
<label for="full_name">Full Name</label><br>
<input type="text" class="textbox" id="full_name" name="full_name" placeholder="Enter Full Name" maxlength="100" autocomplete="off" />
<br>
<br>
<label for="email">Email</label><br>
<input type="email" class="textbox" id="email" name="email" placeholder="Enter Email" maxlength="100" autocomplete="off" />
<br><br><br>
<button type="submit" class="btn-donate">Donate Now</button>
<input type="hidden" name="donation_id" id="donation_id" value="<?php printf(uniqid()); ?>">
<input type="hidden" name="cause_id" id="cause_id" value="<?php the_ID();?>">
<?php wp_reset_postdata(); ?>
<?php wp_nonce_field( ‘cpt_nonce_action’, ‘cpt_nonce_field’ ); ?>
</form>
</div>
<!-- donation end -->
</div>
</div>
<!-- tbl-layout end -->
</div>
</div>
<!-- main_wrapper end -->
<?php get_footer(); ?>
这是捐赠代谢箱代码:
/**
* Generated by the WordPress Meta Box Generator
*/
class Donation_Meta_Box {
private $screens = array(
\'donation\',
);
private $fields = array(
array(
\'id\' => \'donation-id\',
\'label\' => \'donation_id\',
\'type\' => \'number\',
),
array(
\'id\' => \'cause-id\',
\'label\' => \'cause_id\',
\'type\' => \'number\',
),
array(
\'id\' => \'cause-title\',
\'label\' => \'cause_title\',
\'type\' => \'text\',
),
array(
\'id\' => \'donation-currency\',
\'label\' => \'donation_currency\',
\'type\' => \'text\',
),
array(
\'id\' => \'cause-amount\',
\'label\' => \'cause_amount\',
\'type\' => \'number\',
),
array(
\'id\' => \'full-name\',
\'label\' => \'full_name\',
\'type\' => \'text\',
),
array(
\'id\' => \'email\',
\'label\' => \'email\',
\'type\' => \'email\',
),
/*array(
\'id\' => \'donation-permissions\',
\'label\' => \'donation_permissions\',
\'type\' => \'text\',
),
array(
\'id\' => \'subscription\',
\'label\' => \'subscription\',
\'type\' => \'text\',
),*/
);
/**
* Class construct method. Adds actions to their respective WordPress hooks.
*/
public function __construct() {
add_action( \'add_meta_boxes\', array( $this, \'add_meta_boxes\' ) );
add_action( \'save_post\', array( $this, \'save_post\' ) );
}
/**
* Hooks into WordPress\' add_meta_boxes function.
* Goes through screens (post types) and adds the meta box.
*/
public function add_meta_boxes() {
foreach ( $this->screens as $screen ) {
add_meta_box(
\'donations-metabox\',
__( \'Donations Metabox\', \'amref_2017\' ),
array( $this, \'add_meta_box_callback\' ),
$screen,
\'advanced\',
\'default\'
);
}
}
/**
* Generates the HTML for the meta box
*
* @param object $post WordPress post object
*/
public function add_meta_box_callback( $post ) {
wp_nonce_field( \'donations_metabox_data\', \'donations_metabox_nonce\' );
echo \'Donations Metabox Boilerplate\';
$this->generate_fields( $post );
}
/**
* Generates the field\'s HTML for the meta box.
*/
public function generate_fields( $post ) {
$output = \'\';
foreach ( $this->fields as $field ) {
$label = \'<label for="\' . $field[\'id\'] . \'">\' . $field[\'label\'] . \'</label>\';
$db_value = get_post_meta( $post->ID, \'donations_metabox_\' . $field[\'id\'], true );
switch ( $field[\'type\'] ) {
default:
$input = sprintf(
\'<input %s id="%s" name="%s" type="%s" value="%s">\',
$field[\'type\'] !== \'color\' ? \'class="regular-text"\' : \'\',
$field[\'id\'],
$field[\'id\'],
$field[\'type\'],
$db_value
);
}
$output .= $this->row_format( $label, $input );
}
echo \'<table class="form-table"><tbody>\' . $output . \'</tbody></table>\';
}
/**
* Generates the HTML for table rows.
*/
public function row_format( $label, $input ) {
return sprintf(
\'<tr><th scope="row">%s</th><td>%s</td></tr>\',
$label,
$input
);
}
/**
* Hooks into WordPress\' save_post function
*/
public function save_post( $post_id ) {
if ( ! isset( $_POST[\'donations_metabox_nonce\'] ) )
return $post_id;
$nonce = $_POST[\'donations_metabox_nonce\'];
if ( !wp_verify_nonce( $nonce, \'donations_metabox_data\' ) )
return $post_id;
if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
return $post_id;
foreach ( $this->fields as $field ) {
if ( isset( $_POST[ $field[\'id\'] ] ) ) {
switch ( $field[\'type\'] ) {
case \'email\':
$_POST[ $field[\'id\'] ] = sanitize_email( $_POST[ $field[\'id\'] ] );
break;
case \'text\':
$_POST[ $field[\'id\'] ] = sanitize_text_field( $_POST[ $field[\'id\'] ] );
break;
}
update_post_meta( $post_id, \'donations_metabox_\' . $field[\'id\'], $_POST[ $field[\'id\'] ] );
} else if ( $field[\'type\'] === \'checkbox\' ) {
update_post_meta( $post_id, \'donations_metabox_\' . $field[\'id\'], \'0\' );
}
}
}
}
new Donation_Meta_Box;
我将使用活动的id来建立两种帖子类型之间的关系,因此我不会使用诸如post 2 post或Pods之类的插件(除非根据我的问题建议有必要)
第一次海报,谢谢您的时间。