目前我正在使用get_posts
来获取10条最新帖子,但问题是我没有获得所有信息。例如,我无法访问帖子类别或作者。
我想将WP_Query
与get_the_title()
,get_the_post_thumbnail
,get_permalink()
等一起使用。结果必须存储到变量中。
我的设置:
$latest_posts = get_posts( array(
'category_name' => 'Allgemein',
'posts_per_page' => 10,
"orderby" => "date",
"order" => "DESC"
) );
// Post 1 -- Category: Allgemein
if ( isset( $latest_posts[0] ) ) { // array zero-based index.
// $post1_category = $latest_posts[0]->post_category;
$post1_date = $latest_posts[0]->post_date;
$post1_title = $latest_posts[0]->post_title;
// $post1_tags = $latest_posts[0]->post_tags;
$post1_author = $latest_posts[0]->post_author;
$post1_content = $latest_posts[0]->post_content;
$post1_thumbnail = $latest_posts[0]->get_the_post_thumbnail;
}
// Post 2 -- Category: Allgemein
if ( isset( $latest_posts[1] ) ) {
// $post2_category = $latest_posts[1]->post_category;
$post2_date = $latest_posts[1]->post_date;
$post2_title = $latest_posts[1]->post_title;
// $post2_tags = $latest_posts[1]->post_tags;
$post2_author = $latest_posts[1]->post_author;
$post2_content = $latest_posts[1]->post_content;
$post2_thumbnail = $latest_posts[1]->get_the_post_thumbnail;
}
Post3-10
....
在我的index.php
:
<div class="title">
<?php echo $post1_title?>
</div>
答案 0 :(得分:0)
要获取类别,请使用函数wp_get_post_categories
:
$post1_category = wp_get_post_categories($latest_posts[0]->ID);
要获取标记,请使用函数wp_get_post_tags
:
$post1_tags = wp_get_post_tags($latest_posts[0]->ID);
WordPress有更多功能来使用帖子ID检索帖子数据。遵循:
您的变量$latest_posts[X]
是WP_Post Object
,其中包含一些数据。请参阅WP_Post Reference。