ポートフォリオサイトの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(); ?>
…
これはうまくいきませんでした。なぜなら、用語にはすでに何かがタグ付けされている値があり、投稿に年がない場合でも、メタ値はまだ年を出力するため、用語に対しては機能しません(!?) meta_value。
…</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で年を取得しますが、リンク付きの投稿は1つだけで、2006年からの投稿は1つだけです!?
毎年の所属の横にあるがmeta_keyを使用して投稿を取得するにはどうすればよいですか?