我正在构建一个投资组合网站的 index.php,我想在其中按年份和 meta_key 显示帖子。元键用作“特色项目”选项。
一开始,我制作了一个自定义分类法“年份”,其中包含不同的项目年份作为术语,并使用 foreach 来填充年份旁边的帖子。
<?php $years = get_terms('year', 'orderby=name&order=desc&hide_empty=1'); ?>
<?php foreach( $years as $year ) : ?>
<div class="front-index"><p><?php echo $year->name; ?></p></div>
<?php
$year_query = array(
'post_type' => 'works',
'meta_key' => 'post_h2_mask',
'meta_value' => '1',
'taxonomy' => 'year',
'term' => $year->slug );
$year_posts = new WP_Query ($year_query);
?>
<?php while ( $year_posts->have_posts() ) : $year_posts->the_post(); ?>
…
这没有成功,因为术语已经具有标记某些东西的值,并且元值不适用于术语(!?)因为它仍然输出年份,即使帖子没有元值。
…</p>
所以我认为解决方案可能是使用 WordPress 自己的“发布”日期来按年份填充帖子。为此,我找到了一个 mysql 片段并进行了一些操作。
<?php
$years = $wpdb->get_col("
SELECT DISTINCT YEAR(post_date)
FROM $wpdb->posts
WHERE post_status = 'publish'
AND post_type = 'works'
ORDER BY post_date DESC");
?>
<?php foreach( $years as $year ) : ?>
<div class="front-index"><p><?php echo $year; ?></p></div>
<?php
$year_query = array(
'post_type' => 'works',
'meta_key' => 'post_h2_mask',
'meta_value' => '1',
'year' => $year
);
$year_posts = new WP_Query ($year_query);
?>
<?php while ( $year_posts->have_posts() ) : $year_posts->the_post(); ?>
<?php
$attachments = new Attachments( 'attachments' );
if( $attachments->exist() ) :
?>
<div class="front-work">
<div class="wraptocenter">
<a href="<?php the_permalink() ?>" rel="bookmark">
<?php the_post_thumbnail('front-thumbnail'); ?>
<p><?php the_title(); ?><br /><span class="image-count">
<?php
$project_cats = get_the_terms($post->ID, 'medium');
$project_cats = array_values($project_cats);
for($cat_count=0; $cat_count<count($project_cats); $cat_count++) {
echo '<span>'.$project_cats[$cat_count]->name.'</span>';
if ($cat_count<count($project_cats)-1){
echo ', ';
}
}
?>,
<span><?php echo $attachments->total(); ?> images</span></span></p>
</a>
</div>
</div>
<?php endif; endwhile; ?>
<?php endforeach ?>
这在他们的 div 中得到了年份,但它只显示了一个带有链接的帖子,而且这只是 2006 年的一个帖子!?
如何使用 meta_key 在他们的年度财产旁边获取帖子?