0

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

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

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 中。

任何帮助是极大的赞赏。

4

2 回答 2

1

这是我在评论中的意思的一个例子。我没有测试代码,所以它可能包含一些语法错误。我将其更改Twig_SimpleFilterTwig_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 %}
于 2016-05-14T09:03:21.167 回答
0

好的。更多地研究 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 }}
于 2016-05-12T18:27:26.120 回答