Php 如何在Wordpress中检索最新文章的标题?
我在一个基于Wordpress的网站上工作。我的客户希望在主页的标题下显示最新的文章片段。我能够做到这一点,但不是在循环中获取文章的标题,而是获取页面标题并显示在那里 这是我的密码:Php 如何在Wordpress中检索最新文章的标题?,php,wordpress,Php,Wordpress,我在一个基于Wordpress的网站上工作。我的客户希望在主页的标题下显示最新的文章片段。我能够做到这一点,但不是在循环中获取文章的标题,而是获取页面标题并显示在那里 这是我的密码: <?php $postslist = get_posts('numberposts=1&order=DESC&orderby=date'); foreach ($postslist as $post) : setup_postdata($post); ?> <
<?php
$postslist = get_posts('numberposts=1&order=DESC&orderby=date');
foreach ($postslist as $post) :
setup_postdata($post);
?>
<div class="entry">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_time(get_option('date_format')) ?><?php if (has_post_thumbnail() ) {
the_post_thumbnail();
} the_excerpt(); ?>
</div>
<?php endforeach; ?>
我还尝试了获取标题()代码>但它没有工作得很好
来自wordpress codex
如果要使用循环
$args = array('posts_per_page' => 1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
?>
<div class="entry">
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_time(get_option('date_format')) ?> <?php the_excerpt(); ?>
</div>
<?php
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
$args=array('posts\u per\u page'=>1);
//询问
$thew_query=newwp_query($args);
//环路
if($the\u query->have\u posts()){
while($the\u query->have\u posts()){
$the_query->the_post();
?>
请使用这个:
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
<div class="entry">
<h3><a href="<?php echo get_permalink($recent['ID']); ?>"><?php echo $recent['post_title'] ?></a></h3>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php echo get_the_post_thumbnail( $recent['ID'] ); ?>
<?php echo get_excerpt($recent['ID']); ?>
</div>
}
?>
}
?>
它成功了。有一个名为smart post List的插件不允许它工作。现在它工作正常。
我的错误是没有禁用它并测试它。我的代码是正确的。我用你的代码替换了我的代码,所以是的,只需用第二个代码块替换你的代码。它不起作用,我将代码添加到php开始和结束标记中,但它在我的网站上不起作用…什么不起作用?你的帖子发布了吗?发布日期在现在之前?在这里,对于我在编辑后的回答中给出的站点。那么摘录呢?为摘录而修改。为帖子特色图片而修改。站点上没有出现与此相关的内容。请看,我没有将此插入index.php。我将此插入索引页面上的小部件中,因此我需要在其中调用帖子。我的代码确实在那里调用它它无法检索标题,这是我的问题。我认为在我的情况下,你的代码的问题是,它试图检索文章,假设它是索引页或什么的,因为如果它甚至在文章上,它会进行div,但它不是我检查时看到的。
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
<div class="entry">
<h3><a href="<?php echo get_permalink($recent['ID']); ?>"><?php echo $recent['post_title'] ?></a></h3>
<?php the_time( get_option( 'date_format' ) ); ?>
<?php echo get_the_post_thumbnail( $recent['ID'] ); ?>
<?php echo get_excerpt($recent['ID']); ?>
</div>
}
?>