您实际上无法将与分类法相关的参数传递给get_posts()
。 (编辑:实际上,是的,您可以。该Codex尚不清楚。从源头上看,get_posts()
它只是一个包装WP_Query()
。)您可以传递元键/值和post 类型,但不能传递诸如post之类的分类法格式。所以对于这一行:
$myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');
我建议使用WP_Query()
而不是get_posts()
:
$myposts = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-aside',
'post-format-audio',
'post-format-chat',
'post-format-gallery',
'post-format-image',
'post-format-link',
'post-format-quote',
'post-format-status',
'post-format-video'
),
'operator' => 'NOT IN'
)
)
) );
注意:是的,这是很多嵌套数组。税务查询可能很棘手。
下一步是修改循环的打开/关闭语句。更改这些:
<?php foreach($myposts as $post) : ?>
<?php /* loop markup goes here */ ?>
<?php endforeach; ?>
...对此:
<?php if ( $myposts->have_posts() ) : while ( $myposts->have_posts() ) : $myposts->the_post(); ?>
<?php /* loop markup goes here */ ?>
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
您的实际循环标记应该能够保持不变,只是您不再需要调用setup_postdata( $post )
:
<?php
$year = mysql2date('Y', $post->post_date);
$month = mysql2date('n', $post->post_date);
$day = mysql2date('j', $post->post_date);
?>
<p>
<span class="the_article">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</span>
<span class="the_day">
<?php the_time('j F Y'); ?>
</span>
</p>
因此,将它们放在一起:
<?php
// Only query posts with the
// "standard" post format, which
// requires *excluding* all other
// post formats, since neither the
// "post_format" taxonomy nor the
// "post-format-standard" taxonomy term
// is applied to posts without
// defined post formats
$myposts = new WP_Query( array(
'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => array(
'post-format-aside',
'post-format-audio',
'post-format-chat',
'post-format-gallery',
'post-format-image',
'post-format-link',
'post-format-quote',
'post-format-status',
'post-format-video'
),
'operator' => 'NOT IN'
)
)
) );
// Open the loop
if ( $myposts->have_posts() ) : while ( $myposts->have_posts() ) : $myposts->the_post(); ?>
$year = mysql2date('Y', $post->post_date);
$month = mysql2date('n', $post->post_date);
$day = mysql2date('j', $post->post_date);
?>
<p>
<span class="the_article">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</span>
<span class="the_day">
<?php the_time('j F Y'); ?>
</span>
</p>
<?php
// Close the loop
endwhile; endif;
// Reset $post data to default query
wp_reset_postdata();