我的wordpress博客上有一个静态主页。我不想在所有其他页面的选定区域列出最新的帖子标题。所以我有这个代码。
<?php
$args = array(
'showposts' => '1'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
?>
<header>
<h2 class="latestPost"><a href="#"><?php the_title(); ?></a></h2>
</header>
<?php
} else {
// no posts found
}
wp_reset_postdata();
?>
这不会像我希望的那样显示最新的帖子标题,它显示访问者所在页面的标题。
我做错了什么?
像这样尝试
$args = array(
'showposts' => '1'
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<header>
<h2 class="latestPost"><a href="#"><?php the_title(); ?></a></h2>
</header>
<?php
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
<?php query_posts('showposts=1'); ?>
<?php if (have_posts()) : the_post(); ?>
<header>
<h2 class="latestPost"><a href="#"><?php the_title(); ?></a></h2>
</header>
<?php
} else {
// no posts found
}
?>