我有一个相当复杂的问题要解决,这让我在最后一天左右感到困惑。
基本上,我有两种自定义帖子类型“会话”和“食谱”。这些在 WordPress 的后端显示得很好。
会议附带了几个元框,“介绍视频”、“会议视频”、“目标”、“健身水平”和“特色食谱”。
“精选食谱”元框很特殊,因为它查询 WordPress 数据库以获取“食谱”自定义帖子类型中的所有帖子,然后使用这些值来填充选择框。
现在,正如您从所附的屏幕截图中看到的那样,配方自定义帖子类型的选择框正在拉出正确的项目和 ID,它还保存数据并拉回正确的选择并将其与正确的帖子 ID 关联“会话”帖子类型。伟大的!
帖子中的其他字段出现问题,它们似乎使用第一个配方 ID 作为提取每个字段数据的值,而不是“会话”自定义帖子类型 ID。
现在我可以通过基于特定查询参数编写各种 while 循环来使其工作,但这会产生另一个问题。数据保存在正确的位置,但它使用第一个“会话”cpt ID 来拉回所有帖子的数据。
现在,我相信这与“配方”元框的创建有关,只要您从 functions.php 文件中删除此函数及其相关函数,一切都会正常运行。
我想我应该使用 for 循环而不是 while 循环,我现在要试试这个。
希望你们中的一些 PHP 专家能够为我指明正确的方向。我已经包含了我的 functions.php 文件和来自管理员的屏幕截图。干杯。
<?php
add_action( 'init', 'tfp_session_cpt' );
function tfp_session_cpt() {
register_post_type( 'session',
array(
'labels' => array(
'name' => __( 'Sessions' ),
'singular_name' => __( 'Session' )
),
'public' => true,
'has_archive' => true,
)
);
}
add_action( 'init', 'tfp_recipe_cpt' );
function tfp_recipe_cpt() {
register_post_type( 'recipe',
array(
'labels' => array(
'name' => __( 'Recipes' ),
'singular_name' => __( 'Recipe' )
),
'public' => true,
'has_archive' => true,
)
);
}
/*add_action( 'init', 'tfp_tip_cpt' );
function tfp_tip_cpt() {
register_post_type( 'tip',
array(
'labels' => array(
'name' => __( 'Tips' ),
'singular_name' => __( 'Tip' )
),
'public' => true,
'has_archive' => true,
)
);
}*/
add_action( 'add_meta_boxes', 'tfp_add_introduction_video' );
add_action( 'add_meta_boxes', 'tfp_add_session_video' );
add_action( 'add_meta_boxes', 'tfp_add_goals' );
add_action( 'add_meta_boxes', 'tfp_add_levels' );
add_action( 'add_meta_boxes', 'tfp_add_recipes' );
/*add_action( 'add_meta_boxes', 'tfp_add_tips' );*/
add_action( 'save_post', 'tfp_save_introduction_video');
add_action( 'save_post', 'tfp_save_session_video');
add_action( 'save_post', 'tfp_save_goals');
add_action( 'save_post', 'tfp_save_levels');
add_action( 'save_post', 'tfp_save_recipes');
/*add_action( 'save_post', 'tfp_save_tips');*/
function tfp_add_introduction_video() {
add_meta_box('tfp_introduction_video', 'Introduction Video', 'tfp_introduction_video_html', 'session');
}
function tfp_add_session_video() {
add_meta_box('tfp_session_video', 'Session Video', 'tfp_session_video_html', 'session');
}
function tfp_add_goals() {
$types = array ('session');
foreach ($types as $type) {
add_meta_box('tfp_goals', 'Goals', 'tfp_goals_html', $type);
}
}
function tfp_add_levels() {
add_meta_box('tfp_levels', 'Fitness Levels', 'tfp_levels_html', 'session');
}
function tfp_add_recipes() {
add_meta_box('tfp_recipes', 'Select a Featured Recipe', 'tfp_recipes_html', 'session', 'side');
}
function tfp_add_tips() {
add_meta_box('tfp_tips', 'Tips', 'tfp_tips_html', 'session', 'side');
}
function tfp_introduction_video_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
global $post;
$introductionVideo = get_post_meta( $postID, 'introduction_video_embed_code', true );
echo $post->ID;
?>
<label for="introduction_video_embed_code">YouTube Embed Code</label>
<textarea class="large-text code" id="introduction_video_embed_code" name="introduction_video_embed_code" value="<?php echo $introductionVideo; ?>"><?php echo get_post_meta( $post->ID, 'introduction_video_embed_code', true ); ?></textarea>
<?php }
function tfp_session_video_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
global $post;
$sessionVideo = get_post_meta( $post->ID, 'session_video_embed_code', true ); ?>
<?php echo $post->ID; ?>
<label for="session_video_embed_code">YouTube Embed Code</label>
<textarea class="large-text code" id="session_video_embed_code" name="session_video_embed_code" value="<?php echo $sessionVideo; ?>"><?php echo $sessionVideo; ?></textarea>
<?php }
function tfp_goals_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
$goals = get_post_meta( $post->ID, 'goals', true ); ?>
<?php echo $post->ID; ?>
<label for="weight_loss">
<input type="checkbox" name="goal[]" id="weight_loss" value="Weight Loss" <?php if (in_array('Weight Loss', $goals)) echo "checked='checked'"; ?> />
<?php _e("Weight Loss"); ?>
</label>
<br />
<label for="improve_fitness">
<input type="checkbox" name="goal[]" id="improve_fitness" value="Improve Fitness" <?php if (in_array('Improve Fitness', $goals)) echo "checked='checked'"; ?> />
<?php _e("Improve Fitness"); ?>
</label>
<br />
<label for="improve_health">
<input type="checkbox" name="goal[]" id="improve_health" value="Improve Health" <?php if (in_array('Improve Health', $goals)) echo "checked='checked'"; ?> />
<?php _e("Improve Health"); ?>
</label>
<br />
<?php }
function tfp_levels_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
$levels = get_post_meta( $post->ID, 'levels', true ); ?>
<?php echo $post->ID; ?>
<label for="beginner">
<input type="checkbox" name="level[]" id="beginner" value="Beginner" <?php if (in_array('Beginner', $levels)) echo "checked='checked'"; ?> />
<?php _e("Beginner"); ?>
</label>
<br />
<label for="advanced">
<input type="checkbox" name="level[]" id="advanced" value="Advanced" <?php if (in_array('Advanced', $levels)) echo "checked='checked'"; ?> />
<?php _e("Advanced"); ?>
</label>
<br />
<?php }
function tfp_recipes_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
global $post;
$recipes = get_post_meta( $post->ID, 'recipe_name', true );
$recipeArgs = array(
'post_type' => 'recipe'
);
$recipeQuery = new WP_Query($recipeArgs); ?>
<?php echo $post->ID; ?>
<select name="recipe_names" id="recipes">
<option value="null">Select a Featured Recipe</option>
<?php while ( $recipeQuery->have_posts() ) : $recipeQuery->the_post(); ?>
<option id="<?php echo $post->ID; ?>" value="<?php the_title(); ?>" <?php $title = get_the_title(); if($recipes == $title) echo "selected='selected'";?>><?php the_title(); ?></option>
<?php endwhile; ?>
</select>
<?php }
/*function tfp_tips_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
global $post;
$tipArgs = array(
'post_type' => 'tip'
);
$tipQuery = new WP_Query($tipArgs); ?>
<?php echo $post->ID; ?>
<?php while ( $tipQuery->have_posts() ) : $tipQuery->the_post();
$tipID = 'tip_' . $post->ID;
$tips = get_post_meta( $post->ID, $tipID, true );?>
<?php echo $tipID; ?>
<label for="<?php echo $tipID; ?>">
<input type="checkbox" name="<?php echo $tipID; ?>" id="<?php echo $tipID; ?>" value="1" <?php if ($tips == 1) echo "checked='checked'"; ?> />
<?php the_title(); ?>
</label>
<br />
<?php endwhile;
}*/
function tfp_save_introduction_video($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post_id, 'introduction_video_embed_code', $_POST['introduction_video_embed_code']);
}
function tfp_save_session_video($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post_id, 'session_video_embed_code', $_POST['session_video_embed_code']);
}
function tfp_save_goals($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post_id, 'goals', $_POST['goal']);
}
function tfp_save_levels($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post_id, 'levels', $_POST['level']);
}
function tfp_save_recipes($post_id) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
update_post_meta($post_id, 'recipe_name', $_POST['recipe_names']);
}
/*function tfp_save_tips($post) {
if ( !current_user_can( 'edit_post', $post_id ) ) { return false; }
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( ! wp_verify_nonce( $_POST['custom_post_type'], plugin_basename(__FILE__) ) ) return;
global $post;
$tipArgs = array(
'post_type' => 'tip'
);
$tipQuery = new WP_Query($tipArgs);
while ( $tipQuery->have_posts() ) : $tipQuery->the_post();
$tipID = 'tip_' . $post->ID;
update_post_meta($post->ID, $tipID, $_POST[$tipID]);
endwhile;
}*/
// Extra user profile fields, specifically goal and fitness level checkboxes and radio buttons
add_action( 'show_user_profile', 'gaf_user_profile_fields' );
add_action( 'edit_user_profile', 'gaf_user_profile_fields' );
// Function to generate field HTML
function gaf_user_profile_fields( $user ) { ?>
<h3><?php _e("Goals and Fiteness Level", "blank"); ?></h3>
<table class="form-table">
<tr>
<th scope="row"><?php _e("Goals"); ?></th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e("Goals"); ?></span>
</legend>
<?php $single = true; ?>
<?php $goals = get_user_meta( $user->ID, 'goals', $single ); ?>
<label for="weight_loss">
<input type="checkbox" name="goal[]" id="weight_loss" value="Weight Loss" <?php if (in_array('Weight Loss', $goals)) echo "checked='checked'"; ?> />
<?php _e("Weight Loss"); ?>
</label>
<br />
<label for="improve_fitness">
<input type="checkbox" name="goal[]" id="improve_fitness" value="Improve Fitness" <?php if (in_array('Improve Fitness', $goals)) echo "checked='checked'"; ?> />
<?php _e("Improve Fitness"); ?>
</label>
<br />
<label for="improve_health">
<input type="checkbox" name="goal[]" id="improve_health" value="Improve Health" <?php if (in_array('Improve Health', $goals)) echo "checked='checked'"; ?> />
<?php _e("Improve Health"); ?>
</label>
<br />
</fieldset>
</td>
</tr>
<tr>
<?php $fitnessLevel = get_the_author_meta('fitness_level', $user->ID); ?>
<th scope="row"><?php _e("Fitness Level"); ?></th>
<td>
<fieldset>
<legend class="screen-reader-text">
<span><?php _e("Fitness Level"); ?></span>
</legend>
<input class="tog" type="radio" name="fitness_level" value="beginner" <?php if ($fitnessLevel == 'beginner') { ?>checked="checked"<?php } ?> /><label>Beginner</label>
<br />
<input class="tog" type="radio" name="fitness_level" value="advanced" <?php if ($fitnessLevel == 'advanced') { ?>checked="checked"<?php } ?> /><label>Advanced</label>
<br />
</fieldset>
</td>
</tr>
</table>
<?php }
// Function and actions to save the data from the fields
add_action( 'personal_options_update', 'save_gaf_user_profile_fields' );
add_action( 'edit_user_profile_update', 'save_gaf_user_profile_fields' );
function save_gaf_user_profile_fields( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) ) { return false; }
update_user_meta( $user_id, 'goals', $_POST['goal'] );
update_user_meta( $user_id, 'fitness_level', $_POST['fitness_level'] );
}
?>
[编辑]
我当然越来越接近这个功能,但它并不完全正确,因为它正在通过同一个标题。
function tfp_recipes_html($post) {
// Nonce field for some validation
wp_nonce_field( plugin_basename( __FILE__ ), 'session' );
$recipeArgs = array(
'post_type' => 'recipe'
);
$recipeQuery = get_posts($recipeArgs);
$recipeName = get_post_meta( $post->ID, 'recipe_name', true );
$recipeTitle[] = get_query_var('name');
echo $recipeTitle;
?>
<select name="recipe_names" id="recipes">
<option value="null">Select a Featured Recipe</option>
<?php foreach ( $recipeQuery as $recipe ) : setup_postdata($recipeQuery); ?>
<option id="<?php echo $recipe->ID; ?>" value="<?php echo $recipeTitle; ?>" <?php if($recipeName == $recipeTitle) echo "selected='selected'";?>><?php echo $recipeName; ?></option>
<?php endforeach; ?>
</select>
<?php }