我正在开发一个wordpress插件,用于管理和显示事件的时间表和时间表。
您可以添加事件时段(帖子类型称为“ tcode_event-day”)并分配给某些特定的活动日(帖子类型称为“ tcode_event-day”)
然后,您可以使用简码[2code-schedule-draw]显示事件时间表
我正在尝试增加在插件中使用自定义分类法的可能性,以使创建具有不同位置和不同天数的差异时间表成为可能。
例如,我的目标可能是
[2code-schedule-draw pacchetto="tax-slug"]
仅显示用分类法标记的日期和活动时段。
我使用ACF(高级自定义字段)在tcode_event和tcode_event-day中创建我的自定义字段,以设置位置,演讲者等内容
我在自定义帖子类型中添加了自定义分类法,效果很好。
// 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();
});
关于如何在简码上实现我的自定义分类法“ pacchetto”并使其像过滤器一样起作用的任何想法?
谢谢
答案 0 :(得分:0)
您需要允许使用短代码att,请参见下面的修改代码,请注意,如果未指定pacchetto,则我在帖子中添加了后备功能,以便为返回的循环显示内容。将其更改为所需的值,或者为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");