我很难弄清楚如何在Wordpress中使用相同的分类“年”对一组帖子进行分组,而不使用单独的存档模板。
我终于设法让它与Wordpress一起工作,但现在我想把它翻译成木材,而我似乎无法做到这一点。谁能给我一些指示?
这是有效的wordpress代码
$date_now = date('Y-m-d H:i:s');
$ex_terms = get_terms( 'exhibition-year','orderby=name&order=desc' );
foreach ( $ex_terms as $ex_term ) {
$member_group_query = new WP_Query(
array(
'post_type' => 'exhibition',
'tax_query' => array(
array(
'taxonomy' => 'exhibition-year',
'field' => 'slug',
'terms' => array( $ex_term->slug ),
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => 'date_end',
'compare' => '<',
'value' => $date_now,
'type' => 'DATETIME'
)
)
)
);
if($ex_term->slug <= date('Y')){
?>
<h2><?php echo $ex_term->name; ?></h2>
<ul>
<?php
if ( $member_group_query->have_posts() ) : while ( $member_group_query->have_posts() ) : $member_group_query->the_post(); ?>
<li><?php echo the_title(); ?></li>
<?php endwhile; endif; ?>
</ul>
<?php
// Reset things, for good measure
$member_group_query = null;
wp_reset_postdata();
}
}
期望的结果应该与此类似,我只显示标题。但它应该包含整个postdata。
我调查过:
https://timber.github.io/docs/reference/timber-post/ https://timber.github.io/docs/reference/timber-postquery/ 和 https://timber.github.io/docs/reference/timber/#get-terms
请帮助,这让我疯了:)
答案 0 :(得分:0)
对您的查询进行了两处更改
在将其与当前年份进行比较之前转换为integer
类型
$ current = current_time('timestamp'); $ ex_terms = get_terms('exhibition-year','orderby = name&amp; order = desc');
foreach($ ex_terms as $ ex_term):
$member_group_query = new WP_Query(
array(
'post_type' => 'exhibition',
'tax_query' => array(
array(
'taxonomy' => 'exhibition-year',
'field' => 'slug',
'terms' => array( $ex_term->slug ),
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => 'date_end',
'compare' => '<',
'value' => $current
)
)
)
);
if( (int)$ex_term->slug <= date('Y')):
echo '<h2>'.$ex_term->name.'</h2>';
echo '<ul>';
if ( $member_group_query->have_posts() ) :
while ( $member_group_query->have_posts() ) : $member_group_query->the_post();
echo '<li>'.get_the_title().'</li>';
endwhile;
endif;
echo '</ul>';
endif;
$member_group_query = null;
wp_reset_postdata();
endforeach;
希望这有帮助。
答案 1 :(得分:0)
@CKDT这是未经测试的,但这是在Timber中的重写...
$date_now = date('Y-m-d H:i:s');
$ex_terms = Timber::get_terms( 'exhibition-year','orderby=name&order=desc' );
foreach ( $ex_terms as &$ex_term ) {
$member_group_query = Timber::get_posts(
array(
'post_type' => 'exhibition',
'tax_query' => array(
array(
'taxonomy' => 'exhibition-year',
'field' => 'slug',
'terms' => array( $ex_term->slug ),
'operator' => 'IN'
)
),
'meta_query' => array(
array(
'key' => 'date_end',
'compare' => '<',
'value' => $date_now,
'type' => 'DATETIME'
)
)
));
$ex_term->events = $member_group_query;
if( $ex_term->slug <= date('Y') ){
$ex_term->past = true;
}
}
$context['ex_terms'] = $ex_terms;
Timber::render('my-file.twig', $context);
{% for term in ex_terms %}
{% if term.past %}
<h2>{{ term.name }}</h2>
<ul>
{% for post in term.events %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
{% endfor %}