[英]List blog titles on custom page
我的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
}
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.