将 WordPress php 按日期排序事件和按类别分组的代码移动到 Timber Twig 模板中

Moving WordPress php code for ordering events by date and grouping by category into Timber Twig templates

我正在使用 Timber 插件将标准 WordPress 主题移动到 Twig 模板中。

我的目标是按日期列出名为 cpt_shows(事件)的自定义 post 类型,但按艺术家列出和分组。例如:

Artist A
event - April 1
event - May 1
event - June 1

Artist B
event - April 1
event - May 1
event - June 1

Artist C
event - April 1
event - May 1
event - June 1

我没有在我的原始模板中使用带有以下代码的 twig 就完成了这项工作:

$today = current_time('Ymd');
$args = array(
    'orderby' => 'post_title',
    'category_name'  => 'events',
     'exclude' => 28
);
$cats = get_categories( $args );

foreach( $cats as $cat ) :
    $args = array(
        'post_type' => 'cpt_shows',
        'meta_query' => array(
            array(
                'key' => 'date',
                'compare' => '>=',
                'value' => $today,
                'type' => 'NUMERIC,'
            )
        ),
        'meta_key' => 'date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'posts_per_page' => -1,
        'category__in'        => array( $cat->term_id ),
        'ignore_sticky_posts' => 1
    );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        echo '<h2><a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a></h2> ';
        while( $query->have_posts() ) : $query->the_post();
            ?>
            <a href="<?php the_permalink();?>"><?php the_title(); ?></a><br>
            <?php
        endwhile;
        wp_reset_postdata();
    }
endforeach;

我想不通的是如何将它移到我的 Twig 模板中,因为我的逻辑代码中有模板,特别是 'category__in' => array( $cat->term_id ), 正在循环中设置。我在 Twig 中尝试过

    {% for cat in categories %}
        {% for post in loopSetupinContext %}

没有成功。最初有更好的方法吗?简而言之,我有一个输出解决方案,但我不确定如何将其移动到 Timber/Twig.

非常感谢任何帮助。

好的。深入研究 Timber,我发现了这一点,但是,我仍然不确定这是最好的方法,所以如果不是,请发表评论。

我没有尝试将其放入我的 php 模板页面 (archive.php),而是将其作为一个函数并在 functions.php.

中添加了一个过滤器
function add_to_twig($twig) {
/* this is where you can add your own fuctions to twig */
$twig->addExtension(new Twig_Extension_StringLoader());
$twig->addFilter(new Twig_SimpleFilter('events', 'events_listing'));
return $twig;
}


function events_listing() {
$today = current_time('Ymd');
$args = array(
    'orderby' => 'post_title',
    'category_name'  => 'events',
    'exclude' => 28
);
$cats = get_categories( $args );

foreach( $cats as $cat ) :
    $args = array(
        'post_type' => 'cpt_shows',
        'meta_query' => array(
            array(
                'key' => 'date',
                'compare' => '>=',
                'value' => $today,
                'type' => 'NUMERIC,'
            )
        ),
        'meta_key' => 'date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'posts_per_page' => -1,
        'category__in'        => array( $cat->term_id ),
        'ignore_sticky_posts' => 1
    );
    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        echo '<h2><a href="' . get_category_link( $cat->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $cat->name ) . '" ' . '>' . $cat->name.'</a></h2> ';
        while( $query->have_posts() ) : $query->the_post();
            ?>
            <a href="<?php the_permalink();?>"><?php the_title(); ?></a><br>
            <?php
        endwhile;
        wp_reset_postdata();
    }
endforeach;
}

然后在我的 Twig 模板中这样调用它:

{{ post.content|events }}

这是我在评论中的意思的一个例子。 我没有测试代码,因此它可能包含一些语法错误。我将 Twig_SimpleFilter 更改为 Twig_SimpleFuction

function add_to_twig($twig) {
    /* this is where you can add your own fuctions to twig */
    $twig->addExtension(new Twig_Extension_StringLoader());
    $twig->addFunction(new Twig_SimpleFunction('events', 'events_listing'));
    return $twig;
}

function events_listing() {
    $today = current_time('Ymd');
    $args = array(
        'orderby' => 'post_title',
        'category_name'  => 'events',
        'exclude' => 28
    );
    $cats = get_categories( $args );


    //init the array
    $data = array();
    foreach( $cats as $cat ) {
        $args = array(
            'post_type'     => 'cpt_shows',
            'meta_query'    => array(
                                    array(
                                        'key' => 'date',
                                        'compare' => '>=',
                                        'value' => $today,
                                        'type' => 'NUMERIC,'
                                    )
                            ),
            'meta_key'      => 'date',
            'orderby'       => 'meta_value',
            'order'         => 'ASC',
            'posts_per_page'=> -1,
            'category__in'  => array( $cat->term_id ),
            'ignore_sticky_posts' => 1
        );
        $query = new WP_Query( $args );
        if ($query->have_posts()) {
            //Prepare the array to keep track of the category
            //And init an extra array for keeping the posts together
            $data[$cat->term_id] = array(
                'category'  =>  array(
                                    'url'   => get_category_link( $cat->term_id ),
                                    'title' => sprintf( __( "View all posts in %s" ), $cat->name),
                                    'text'  => $cat->name,
                                ),
                'posts'     =>  array(),
            );
            while( $query->have_posts() ){
                $query->the_post();
                //append the post to the array
                $data[$cat->term_id]['posts'][] = array(
                    'url'   => the_permalink(),
                    'text'  => the_title(),
                );
            }
            wp_reset_postdata();
        }
    }
    return $data;
}

树枝

{% for event in events() %}
    <h2><a href="{{ event.category.url }}" title="{{ event.category.title }}">{{ event.category.text }}</a></h2>
    {% for post in event.posts %}
        <a href="{{ post.url }}">{{ post.text }}</a><br />
    {% endfor %}    
{% endfor %}