移动WordPress php代码,按日期排序事件,按类别分组为Timber Twig模板

时间:2016-05-12 14:28:27

标签: wordpress twig timber

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

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

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模板中,因为我的逻辑代码中有模板,特别是&#39; category__in&#39; =&GT;数组($ cat-&gt; term_id),正在循环中设置。我在Twig尝试过像

那样的事情
    {% for cat in categories %}
        {% for post in loopSetupinContext %}

没有成功。是否有更好的方法来执行此操作?简而言之,我有一个输出解决方案,但我不确定如何将其移动到Timber / Twig。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

这是我在评论中的意思的一个例子。 我没有测试代码,所以它可能包含一些语法错误。我将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 %}

答案 1 :(得分:0)

确定。我更深入地研究木材,但是,我仍然不确定这是最好的方法,所以如果没有请评论。

我没有尝试将它放在我的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 }}