在Wordpress Shortcode中添加分类属性

问题描述 投票:1回答:1

我正在开发一个wordpress插件,用于管理和显示事件的时间表和时间表。

您可以添加事件槽(称为“tcode_event”的帖子类型)并分配给某些特定的事件日(称为“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'并使其像过滤器一样工作的任何想法?

谢谢

php wordpress
1个回答
0
投票

您需要允许短代码,请参阅下面修改过的代码,请注意,如果未指定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");
© www.soinside.com 2019 - 2024. All rights reserved.