Php 在Wordpress短代码中添加分类属性
我正在开发一个wordpress插件,用来管理和显示活动的时间表 您可以添加事件槽(post类型称为“tcode_Event”)并分配给某些特定的事件日(post类型称为“tcode_Event-day”) 然后,您可以使用短代码[2代码时间表绘制]显示活动时间表 我试图在插件中添加使用自定义分类法的可能性,以便能够使用不同的时段和不同的日期创建不同的时间表 例如,我的目标可能是Php 在Wordpress短代码中添加分类属性,php,wordpress,Php,Wordpress,我正在开发一个wordpress插件,用来管理和显示活动的时间表 您可以添加事件槽(post类型称为“tcode_Event”)并分配给某些特定的事件日(post类型称为“tcode_Event-day”) 然后,您可以使用短代码[2代码时间表绘制]显示活动时间表 我试图在插件中添加使用自定义分类法的可能性,以便能够使用不同的时段和不同的日期创建不同的时间表 例如,我的目标可能是 [2code-schedule-draw pacchetto="tax-slug"] 仅显示用分类法标记的日期和
[2code-schedule-draw pacchetto="tax-slug"]
仅显示用分类法标记的日期和事件槽
我使用ACF(高级自定义字段)在tcode_事件和tcode_事件日中创建自定义字段,以设置位置、扬声器等内容
我在自定义帖子类型中添加了自定义分类法,效果非常好
// Setup 'pacchetti' taxonomy
add_action('init', function() {
$labels = array(
'name' => _x( 'Pacchetti', 'taxonomy general name' ),
'singular_name' => _x( 'Pacchetto', 'taxonomy singular name' ),
'search_items' => __( 'Search Pacchetti' ),
'all_items' => __( 'All Pacchetti' ),
'parent_item' => __( 'Parent Pacchetti' ),
'parent_item_colon' => __( 'Parent Pacchetti:' ),
'edit_item' => __( 'Edit Pacchetti' ),
'update_item' => __( 'Update Pacchetti' ),
'add_new_item' => __( 'Add New Pacchetto' ),
'new_item_name' => __( 'New Pacchetti Name' ),
'menu_name' => __( 'Pacchetti' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'public' => false,
'show_ui' => true,
'show_admin_column' => false,
'show_in_quick_edit'=> false,
'show_tagcloud' => false,
'show_in_nav_menus' => false,
'query_var' => true,
'rewrite' => array( 'slug' => 'pacchetti' ),
);
register_taxonomy('pacchetti', array('tcode_event','tcode_event-day'), $args); });
现在我可以创建自定义分类法并将其分配给自定义文章类型中的项目
现在我对如何在我的短代码函数中添加一个过滤器很感兴趣,我尝试了好几次,但都没有成功。
这里是原始的短代码函数:
// Initialize schedule shortcode
add_shortcode('2code-schedule-draw', function() {
if (!class_exists('acf')) {
return 'Could not find ACF. Please make sure it\'s installed or the
\'Use embedded ACF\' option is selected in event-schedule settings.';
}
$postArray = array();
$posts = get_posts(array(
'post_type' => 'tcode_event',
'posts_per_page' => -1,
'numberposts' => -1,
'post_status' => 'publish',
'suppress_filters' => false
));
if (!empty($posts)) {
foreach($posts as $post) {
setup_postdata($post);
// field_56b8f1ecb7820 is the key of the array to link the slot to the day
if (have_rows('field_56b8f1ecb7820', $post->ID)) {
while (have_rows('field_56b8f1ecb7820', $post->ID)) {
the_row();
$datePost = get_sub_field('event_date');
if (!$datePost || $datePost->post_status !== 'publish') {
continue;
}
$time = get_sub_field('event_time');
$time_ends = get_sub_field('event_time_ends');
$time_end = date('Y-m-d ') . get_sub_field('event_time_end');
$location = get_sub_field('event_location');
$date = get_field('event_day_date', $datePost->ID);
$date = str_replace('/', '-', $date);
$date = new DateTime($date);
$dateFormatted = $date->format('Y-m-d');
$events = isset($postArray[$dateFormatted]) && isset($postArray[$dateFormatted]['events']) ? $postArray[$dateFormatted]['events'] : array();
$events[] = array(
'time' => $time,
'time_ends' => $time_ends,
'time_end' => $time_end,
'event' => $post,
'location' => !empty($location) ? $location->slug : ''
);
usort($events, function($a, $b) {
$aTime = new DateTime($a['time']);
$bTime = new DateTime($b['time']);
return $aTime->getTimestamp() > $bTime->getTimestamp();
});
$locations = isset($postArray[$dateFormatted]) && isset($postArray[$dateFormatted]['locations']) ? $postArray[$dateFormatted]['locations'] : array();
if (!empty($location)) {
$locationsSanitized = array_map(function($cat) {
return $cat->slug;
}, $locations);
if (!in_array($location->slug, $locationsSanitized)) {
$locations[] = $location;
}
}
usort($locations, function($a, $b) {
$aName = $a->name;
$bName = $b->name;
if (isset($a->term_order) && isset($b->term_order)) {
$aOrder = $a->term_order;
$bOrder = $b->term_order;
if ($aOrder !== $bOrder) {
return $aOrder < $bOrder;
}
}
return $aName < $bName;
});
$postArray[$dateFormatted] = array(
'day' => $datePost,
'events' => $events,
'locations' => $locations
);
}
}
}
wp_reset_postdata();
}
ksort($postArray);
$postArray = array_values($postArray);
$imageFormat = get_field('2code_image_format', 'options');
$daysNum = get_field('2code_number_of_days', 'options');
ob_start();
require TCODE_ES_DIR . '/assets/templates/template.php';
return ob_get_clean();
});
//初始化计划短代码
添加快捷码('2code-schedule-draw',函数(){
如果(!class_存在('acf')){
return“找不到ACF。请确保它已安装或
\在事件日程设置中选择了“使用嵌入式ACF\”选项;
}
$postArray=array();
$posts=获取_posts(数组(
“post_类型”=>“tcode_事件”,
“每页帖子数”=>-1,
“numberposts”=>-1,
“发布状态”=>“发布”,
“抑制_过滤器”=>false
));
如果(!空($posts)){
foreach($posts作为$post){
设置_postdata($post);
//字段_56b8f1ecb7820是阵列的键,用于将插槽链接到日
如果(有行('field_56b8f1ecb7820',$post->ID)){
while(有_行('field_56b8f1ecb7820',$post->ID)){
_行();
$datePost=get_sub_字段('event_date');
如果(!$datePost | |$datePost->post_状态!='publish'){
继续;
}
$time=获取子字段(“事件时间”);
$time_ends=获取子字段(“事件时间_ends”);
$time_end=date('Y-m-d')。获取子字段('event_time_end');
$location=获取子字段(“事件位置”);
$date=get\u字段('event\u day\u date',$datePost->ID);
$date=str_replace(“/”、“-”、$date);
$date=新日期时间($date);
$dateFormatted=$date->format('Y-m-d');
$events=isset($postArray[$dateFormatted])&&isset($postArray[$dateFormatted]['events'])?$postArray[$dateFormatted]['events']:array();
$events[]=数组(
“时间”=>$time,
“time\u ends”=>$time\u ends,
“time\u end”=>$time\u end,
“事件”=>$post,
'location'=>!空($location)?$location->slug:'
);
usort($events,function($a,$b){
$aTime=新日期时间($a['time']);
$b时间=新的日期时间($b['time']);
返回$aTime->getTimestamp()>$bTime->getTimestamp();
});
$locations=isset($postArray[$dateFormatted])&&isset($postArray[$dateFormatted]['locations'])?$postArray[$dateFormatted]['locations']:array();
如果(!空($location)){
$LocationsInitiatized=数组\映射(函数($cat){
返回$cat->slug;
}(五个地点),;
如果(!在数组中($location->slug,$locationsInitiatized)){
$locations[]=$location;
}
}
usort($位置、功能($a、$b){
$aName=$a->name;
$b名称=$b->名称;
if(设置($a->期限订单)和&isset($b->期限订单)){
$AOORDER=$a->term\U order;
$bOrder=$b->term\U order;
如果($aoorder!=$bOrder){
返回$aoorder<$bOrder;
}
}
返回$aName<$bName;
});
$postArray[$dateFormatted]=数组(
“day”=>$datePost,
“事件”=>$events,
“位置”=>$locations
);
}
}
}
wp_reset_postdata();
}
ksort(邮费);
$postArray=数组值($postArray);
$imageFormat=get_字段('2code_image_format','options');
$daysNum=get_字段('2code_number_of_days','options');
ob_start();
需要TCODE_ES_DIR.'/assets/templates/template.php';
返回ob_get_clean();
});
你知道如何在短代码上实现我的自定义分类法“pacchetto”并使其像过滤器一样工作吗
谢谢您需要允许短码ATT,请参阅下面修改的代码,请注意,如果没有指定pacchetto,我为POST添加了一个回退,以便为返回的循环显示内容。将其更改为您希望的任何值,或者为pacchetto设置一个默认值,该值在短代码中未填充时使用:
function code_schedule_draw($atts) {
extract(shortcode_atts(array(
"pacchetto" => ''
), $atts));
if (!class_exists('acf')) {
return 'Could not find ACF. Please make sure it\'s installed or the
\'Use embedded ACF\' option is selected in event-schedule settings.';
}
$postArray = array();
if(empty($pacchetto)) {
$pacchetto = 'posts'
}
$posts = get_posts(array(
'post_type' => $pacchetto,
'posts_per_page' => -1,
'numberposts' => -1,
'post_status' => 'publish',
'suppress_filters' => false
));
if (!empty($posts)) {
foreach($posts as $post) {
setup_postdata($post);
if (have_rows('field_56b8f1ecb7820', $post->ID)) {
while (have_rows('field_56b8f1ecb7820', $post->ID)) {
the_row();
$datePost = get_sub_field('event_date');
if (!$datePost || $datePost->post_status !== 'publish') {
continue;
}
$time = get_sub_field('event_time');
$time_ends = get_sub_field('event_time_ends');
$time_end = date('Y-m-d ') . get_sub_field('event_time_end');
$location = get_sub_field('event_location');
$date = get_field('event_day_date', $datePost->ID);
$date = str_replace('/', '-', $date);
$date = new DateTime($date);
$dateFormatted = $date->format('Y-m-d');
$events = isset($postArray[$dateFormatted]) && isset($postArray[$dateFormatted]['events']) ? $postArray[$dateFormatted]['events'] : array();
$events[] = array(
'time' => $time,
'time_ends' => $time_ends,
'time_end' => $time_end,
'event' => $post,
'location' => !empty($location) ? $location->slug : ''
);
usort($events, function($a, $b) {
$aTime = new DateTime($a['time']);
$bTime = new DateTime($b['time']);
return $aTime->getTimestamp() > $bTime->getTimestamp();
});
$locations = isset($postArray[$dateFormatted]) && isset($postArray[$dateFormatted]['locations']) ? $postArray[$dateFormatted]['locations'] : array();
if (!empty($location)) {
$locationsSanitized = array_map(function($cat) {
return $cat->slug;
}, $locations);
if (!in_array($location->slug, $locationsSanitized)) {
$locations[] = $location;
}
}
usort($locations, function($a, $b) {
$aName = $a->name;
$bName = $b->name;
if (isset($a->term_order) && isset($b->term_order)) {
$aOrder = $a->term_order;
$bOrder = $b->term_order;
if ($aOrder !== $bOrder) {
return $aOrder < $bOrder;
}
}
return $aName < $bName;
});
$postArray[$dateFormatted] = array(
'day' => $datePost,
'events' => $events,
'locations' => $locations
);
}
}
} wp_reset_postdata();
}
ksort($postArray);
$postArray = array_values($postArray);
$imageFormat = get_field('2code_image_format', 'options');
$daysNum = get_field('2code_number_of_days', 'options');
ob_start();
require TCODE_ES_DIR . '/assets/templates/template.php';
return ob_get_clean();
} add_shortcode("code-schedule-draw", "code_schedule_draw");
功能代码\计划\图纸($atts){
提取(短码)附件(数组)(
“pacchetto”=>“
)美元(附件);;
如果(!class_存在('acf')){
return“找不到ACF。请确保它已安装或
\在事件日程设置中选择了“使用嵌入式ACF\”选项;
}
$postArray=array();
如果(空($pacchetto)){
$pacchetto='posts'
}
$posts=获取_posts(数组(
“post_type”=>$pacchetto,
“每页帖子数”=>-1,
“numberposts”=>-1,
“发布状态”=>“发布”,
“抑制_过滤器”=>false
));
如果(!空($posts)){
foreach($posts作为$post){
设置\u postdata($pos)