Updated 10 hours later: 丰富了备选解决方案,并补充了意见。
<?php
/* ******* Built-in post type "Post" ******* */
$arguments_for_posts = array(
\'numberposts\' => 1, // number of posts to get
\'post_type\' => \'post\' // post type
);
// get the array of post objects
$posts = get_posts( $arguments_for_posts );
// iterate through array to get individual posts
foreach( $posts as $post ) {
// Enclosing block having unique ID
echo \'<article id="post-\' . $post->ID . \'">\';
// Post title (property of $post object)
echo \'<h1>\' . $post->post_title . \'</h1>\';
// Post content (property of $post object)
echo $post->post_content;
echo \'</article>\';
}
/* ******* Custom post type "Feature" ******* */
$arguments_for_features = array(
\'numberposts\' => 1,
\'post_type\' => \'features\'
);
$features = get_posts( $arguments_for_features );
foreach( $features as $feature ) {
echo \'<article id="feature-\' . $feature->ID . \'">\';
echo \'<h1>\' . $feature->post_title . \'</h1>\';
echo $feature->post_content;
echo \'</article>\';
}
或者,当循环看起来更熟悉时,可以使用两个WP\\U查询:
<?php
/* ******* Built-in post type "Post" ******* */
// Arguments
$args = array(
\'posts_per_page\' => 1,
\'post_type\' => \'post\',
);
// The query
$query_posts = new WP_Query( $args );
// The loop
while ( $query_posts->have_posts() ) {
$query_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</article>
<?php
} // END while()
// Essential!
// Set the post data back up
wp_reset_postdata();
/* ******* Custom post type "Feature" ******* */
$args = array(
\'posts_per_page\' => 1,
\'post_type\' => \'feature\',
);
$query_features = new WP_Query( $args );
while ( $query_features->have_posts() ) {
$query_features->the_post();
?>
<article id="feature-<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</article>
<?php
} // END while()
?>
感谢@Qaisar Feroz提供的有用提醒。
有用的链接: