实际上,您不能将与分类法相关的参数传递给get_posts()
.删除>(Edit: actually, yes you can. The Codex is just somewhat unclear. Looking at source, get_posts()
is, at its heart, just a wrapper for 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();