【问题标题】:Add A Taxonomy Attribute In A Wordpress Shortcode在 Wordpress 简码中添加分类属性
【发布时间】:2019-04-04 18:33:12
【问题描述】:

我正在开发一个 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】:

    您需要允许短代码 atts,请参阅以下修改后的代码,请注意,如果未指定 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");
    

    【讨论】:

      猜你喜欢
      • 2017-03-05
      • 2018-07-15
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 2014-04-26
      • 2020-06-30
      • 2014-03-17
      • 2021-03-30
      相关资源
      最近更新 更多