这有点取决于你想要什么。如果您想创建一个“帖子页面”——换句话说,创建一个新的页面模板文件——您可以在该页面上创建一个辅助循环。
The codex 这里有一个例子,这是另一个非常简单的例子。
<?php
/*
Template Name: Page of Posts
*/
get_header();
?>
<?php while( have_posts() ): the_post(); /* start main loop */ ?>
<h1><?php the_title(); ?></h1>
<?php
/* Start Secondary Loop */
$other_posts = new WP_Query( /*maybe some args here? */ );
while( $others_posts->have_posts() ): $other_posts->the_post();
?>
You can do anything you would in the main loop here and it will
apply to the secondary loop\'s posts
<?php
endwhile; /* end secondary loop */
wp_reset_postdata(); /* Restore the original queried page to the $post variable */
?>
<?php endwhile; /* End the main loop */ ?>
如果您正在寻找可以放入任何页面的内容,最好的解决方案是
shortcode. 您需要创建一个短代码,以获取多篇文章并将其返回到列表中(或任何您想要的内容)。例如:
<?php
add_action( \'init\', \'wpse36453_register_shortcode\' );
/**
* Registers the shortcode with add_shortcode so WP knows about it.
*/
function wpse36453_register_shortcode()
{
add_shortcode( \'wpse36453_posts\', \'wpse36453_shortcode_cb\' );
}
/**
* The call back function for the shortcode. Returns our list of posts.
*/
function wpse36453_shortcode_cb( $args )
{
// get the posts
$posts = get_posts(
array(
\'numberposts\' => 3
)
);
// No posts? run away!
if( empty( $posts ) ) return \'\';
/**
* Loop through each post, getting what we need and appending it to
* the variable we\'ll send out
*/
$out = \'<ul>\';
foreach( $posts as $post )
{
$out .= sprintf(
\'<li><a href="%s" title="%s">%s</a></li>\',
get_permalink( $post ),
esc_attr( $post->post_title ),
esc_html( $post->post_title )
);
}
$out .= \'</ul>\';
return $out;
}