在自定义插件中,我有一个WPQuery,如下所示:
$sticky = get_option( \'sticky_posts\' );
$args = array(
\'post_type\' => \'mission\',
\'ignore_sticky_posts\' => 1,
\'orderby\' => \'date\',
\'post__not_in\' => $sticky,
\'posts_per_page\' => $count
);
$latest_missions = new WP_Query( $args );
if ( $latest_missions->have_posts() ) {
while ( $latest_missions->have_posts() ) {
$latest_missions->the_post();
get_template_part( \'content-list\', get_post_format() );
}
wp_reset_postdata();
}
以及相应的模板部分:
<li id="post-<?php the_ID(); ?> " class="future-event"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( \'Permalink to %s\', \'twentyeleven\' ), the_title_attribute( \'echo=0\' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></li>
我唯一的问题是,这不会生成语义正确的HTML,因为列表的语义不正确。
有没有办法,我可以创建一个<ul></ul>
当我的循环完成时,列表中会有哪些内容?
比尔,麦贝克
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成
当然有。就这样做吧:
$sticky = get_option( \'sticky_posts\' );
$args = array(
\'post_type\' => \'mission\',
\'ignore_sticky_posts\' => 1,
\'orderby\' => \'date\',
\'post__not_in\' => $sticky,
\'posts_per_page\' => $count
);
$latest_missions = new WP_Query( $args );
if ( $latest_missions->have_posts() ) {
echo \'<ul>\'; // prints opening ul tag before list items
while ( $latest_missions->have_posts() ) {
$latest_missions->the_post();
get_template_part( \'content-list\', get_post_format() );
}
echo \'</ul>\'; // prints closing ul tag after list items
wp_reset_postdata();
}