如何在wordpress中显示页面上的类别标题。我想在我的主页上显示类别标题,然后链接到该类别中的第一篇文章。我还想展示该类别中第一篇帖子的图片。
我能做到吗?我在想wp\\U查询?
如何在wordpress中显示页面上的类别标题。我想在我的主页上显示类别标题,然后链接到该类别中的第一篇文章。我还想展示该类别中第一篇帖子的图片。
我能做到吗?我在想wp\\U查询?
您需要合并get_categories()
和wp_query
首先,您应该获取所有类别并循环遍历其中的每一个类别。在回路内部,您需要wp_query
获取每个类别最新帖子的标题和图像。最后,您应该重置post数据。
将以下代码放在主页模板中:
$args = array (
\'hide_empty\' => 1,
);
$terms = get_categories( $args );
foreach ( $terms as $term ) {
$query = new WP_Query( array( \'post_type\' => \'post\', \'cat\' => $term->term_id, \'posts_per_page\' => 1 ) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$postlink = get_the_permalink( $query->ID );
$postimage = wp_get_attachment_url( get_post_thumbnail_id( $query->ID ) );
echo \'<h4><img src="\'.$postimage.\'"><a href="\'.$postlink.\'">\'.$term->name.\'</a></h4>\';
}
}
// Restore original Post Data
wp_reset_postdata();
}
嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post