Php Wordpress打印最新帖子
我有一个静态页面,其中10个容器位于index.php文件的不同位置,我想显示10篇最新文章。我想像这样回显帖子的数据(标题、文本、缩略图、作者、时间):Php Wordpress打印最新帖子,php,arrays,wordpress,variables,Php,Arrays,Wordpress,Variables,我有一个静态页面,其中10个容器位于index.php文件的不同位置,我想显示10篇最新文章。我想像这样回显帖子的数据(标题、文本、缩略图、作者、时间): <div class="text"> <?php echo '$textfrompost3'; ?> </div> <div class="title"> <?php ec
<div class="text">
<?php
echo '$textfrompost3';
?>
</div>
<div class="title">
<?php
echo '$titlefrompost3';
?>
</div>
...
...
...
...
我的PHP文件:
<?php
// the query
$the_query = new WP_Query( array(
'category_name' => 'Allgemein',
'posts_per_page' => 10,
"orderby" => "date",
"order" => "DESC"
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
// insert values into variables
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
//在变量中插入值
提前感谢,我感谢您提供的任何帮助。我只需将您对
WP\u Query
的调用替换为get\u posts()
,它将提供一系列post对象
$latest_posts = get_posts( array(
'category_name' => 'Allgemein',
'posts_per_page' => 10,
) );
在上面的例子中,我已经删除了order
和orderby
。虽然要显示的帖子数量可能有不同的全局设置,但不太可能修改默认顺序
文件:
假设我想输出第三篇文章的标题,我可以执行以下操作:
// let's be sure we have a third post.
if ( isset( $latest_posts[2] ) ) {
echo $latest_posts[2]->post_title; // remember arrays have a zero-based index.
}
不工作的可能重复,因为它在一个循环中打印彼此紧挨着的帖子我想要变量中的10篇最新帖子的数据或其他什么,那么可能吗?-这就是谷歌在搜索“将WP_查询结果转换为变量”时返回的结果。我如何获取文章作者或缩略图?:/post_作者不存在。我想使用get_the_author等,但它不起作用。