我在这里有一个技能发展项目:https://blog.unicornpoint.net/wp/safe-foods-meal-generator-diet-assistant/ . 这是一个自定义模板页面。我使用自定义贴子UI来定制贴子类型的膳食,使用ACF来定制一些字段。随着时间的推移,一些食物不再显示在页面上。我在底部列出了膳食,具体取决于膳食的大小和膳食组,当您单击按钮时,这些膳食组将使用jquery、ajax和php显示。这两种方法都会在一段时间后停止显示某些餐点类型。它的类型或大小并不总是相同的。这是某种缓存吗?我仍在学习这一切,我需要一些帮助。
Jquery脚本
<div class="mx-auto meal-icons-container">
<div class="row p-2 mx-auto">
<div class="col-6 col-md-3">
<img src="https://blog.unicornpoint.net/wp/wp-content/uploads/2020/02/panic.png" id="emergency-icon" width="75" height="auto" alt="Emergency Meals" class="meal-icon"><span class="meal-icon-caption"> Emergency</span>
</div>
<script>
jQuery(document).ready(function($){
$(\'#emergency-icon\').click(function(){
$.ajax({
method: \'GET\',
url: ajaxurl, // we send all WP ajax requests to this file, it will load all WP\'s files, plugins. It\'s WP Ajax approach.
data: {
action: \'load_emergency_meals\' // you can name it whatever just use the same in PHP binding ajax action.
}
}).done(function(data){
$("#meal-results").html(data);
});
});
});
</script>
函数中的函数。php
// load emergency meals button
function load_emergency_meals() {
?>
<h2>
Emergency Binge Control Meals
</h2>
<div class="justify-content-center d-inline-flex flex-wrap text-center p-2">
<?php
$args = array(\'post_type\' => \'meal\');
$meals = new WP_Query($args);
if($meals->have_posts()) : while($meals->have_posts()) : $meals->the_post();
$meal_type = get_field(\'meal_type\');
if ( $meal_type && in_array(\'emergency\', $meal_type)){?>
<div class="text-center p-2 meal-size-posts">
<h4><?php the_field(\'meal_name\'); ?></h4>
<img src="<?php echo get_field(\'meal_icon\'); ?>" class="meal-icon">
<p class="meal-description"><?php the_field(\'meal_description\'); ?></p>
<h4><?php the_field(\'meal_calories\'); ?> calories</h4>
</div>
<?php } ?>
<?php endwhile; endif; ?>
</div>
<?php
die();
}
功能中的操作。php
// this is where we bind our function to `load_meals` action name.
// the one with `_nopriv` prefix in wp action name is for not logged in users, and without it for logged in users.
add_action(\'wp_ajax_load_emergency_meals\', \'load_emergency_meals\');
add_action(\'wp_ajax_nopriv_load_emergency_meals\', \'load_emergency_meals\');
自定义帖子类型设置
https://blog.unicornpoint.net/wp/wp-content/uploads/2020/03/meals-cpt-settings.jpg自定义字段设置
if( function_exists(\'acf_add_local_field_group\') ):
acf_add_local_field_group(array(
\'key\' => \'group_5e590c435c453\',
\'title\' => \'Meal\',
\'fields\' => array(
array(
\'key\' => \'field_5e591fd27bd3f\',
\'label\' => \'Name\',
\'name\' => \'meal_name\',
\'type\' => \'text\',
\'instructions\' => \'What is this meal called?\',
\'required\' => 1,
\'conditional_logic\' => 0,
\'wrapper\' => array(
\'width\' => \'\',
\'class\' => \'\',
\'id\' => \'\',
),
\'default_value\' => \'\',
\'placeholder\' => \'name the meal\',
\'prepend\' => \'\',
\'append\' => \'\',
\'maxlength\' => \'\',
),
array(
\'key\' => \'field_5e5920a99f373\',
\'label\' => \'Meal Icon\',
\'name\' => \'meal_icon\',
\'type\' => \'image\',
\'instructions\' => \'Choose an icon for this food.\',
\'required\' => 1,
\'conditional_logic\' => 0,
\'wrapper\' => array(
\'width\' => \'\',
\'class\' => \'\',
\'id\' => \'\',
),
\'return_format\' => \'url\',
\'preview_size\' => \'medium\',
\'library\' => \'all\',
\'min_width\' => \'\',
\'min_height\' => \'\',
\'min_size\' => \'\',
\'max_width\' => \'\',
\'max_height\' => \'\',
\'max_size\' => \'\',
\'mime_types\' => \'\',
),
array(
\'key\' => \'field_5e592164996b4\',
\'label\' => \'Meal Description\',
\'name\' => \'meal_description\',
\'type\' => \'textarea\',
\'instructions\' => \'Describe the meal.\',
\'required\' => 1,
\'conditional_logic\' => 0,
\'wrapper\' => array(
\'width\' => \'\',
\'class\' => \'\',
\'id\' => \'\',
),
\'default_value\' => \'\',
\'placeholder\' => \'What foods are in this meal and what\\\'s the calorie breakdown?\',
\'maxlength\' => \'\',
\'rows\' => \'\',
\'new_lines\' => \'\',
),
array(
\'key\' => \'field_5e590c7a69cc7\',
\'label\' => \'Calories\',
\'name\' => \'meal_calories\',
\'type\' => \'number\',
\'instructions\' => \'Add how many calories this meal contains.\',
\'required\' => 1,
\'conditional_logic\' => 0,
\'wrapper\' => array(
\'width\' => \'\',
\'class\' => \'\',
\'id\' => \'\',
),
\'default_value\' => \'\',
\'placeholder\' => \'add calories\',
\'prepend\' => \'\',
\'append\' => \'\',
\'min\' => \'\',
\'max\' => \'\',
\'step\' => \'\',
),
array(
\'key\' => \'field_5e6e03dd8dee2\',
\'label\' => \'Meal Type\',
\'name\' => \'meal_type\',
\'type\' => \'checkbox\',
\'instructions\' => \'\',
\'required\' => 1,
\'conditional_logic\' => 0,
\'wrapper\' => array(
\'width\' => \'\',
\'class\' => \'\',
\'id\' => \'\',
),
\'choices\' => array(
\'emergency\' => \'emergency\',
\'basic\' => \'basic\',
\'alcoholic\' => \'alcoholic\',
\'asian\' => \'asian\',
\'mexican\' => \'mexican\',
\'comfort\' => \'comfort\',
\'cooling\' => \'cooling\',
\'sweet\' => \'sweet\',
),
\'allow_custom\' => 0,
\'default_value\' => array(
),
\'layout\' => \'vertical\',
\'toggle\' => 0,
\'return_format\' => \'value\',
\'save_custom\' => 0,
),
array(
\'key\' => \'field_5e6e04b63a353\',
\'label\' => \'Meal Size\',
\'name\' => \'meal_size\',
\'type\' => \'select\',
\'instructions\' => \'\',
\'required\' => 1,
\'conditional_logic\' => 0,
\'wrapper\' => array(
\'width\' => \'\',
\'class\' => \'\',
\'id\' => \'\',
),
\'choices\' => array(
\'small\' => \'small\',
\'medium\' => \'medium\',
\'large\' => \'large\',
\'xl\' => \'xl\',
),
\'default_value\' => array(
),
\'allow_null\' => 0,
\'multiple\' => 0,
\'ui\' => 0,
\'return_format\' => \'value\',
\'ajax\' => 0,
\'placeholder\' => \'\',
),
),
\'location\' => array(
array(
array(
\'param\' => \'post_type\',
\'operator\' => \'==\',
\'value\' => \'meal\',
),
),
),
\'menu_order\' => 0,
\'position\' => \'normal\',
\'style\' => \'default\',
\'label_placement\' => \'top\',
\'instruction_placement\' => \'label\',
\'hide_on_screen\' => array(
0 => \'the_content\',
1 => \'featured_image\',
),
\'active\' => true,
\'description\' => \'\',
));
endif;
这可能是什么问题?感谢您的帮助。谢谢