2024年05月28日 建站教程
wordpress建站中如何在文章页面获取当前分类下最新的10篇文章,下面web建站小编给大家简单介绍一下!
具体语法如下:
<?php $categories = get_the_category(); $category_ids = array(); foreach( $categories as $category ) { $category_ids[] = $category->term_id; } $args = array( 'post_type' => 'post', 'posts_per_page' => 10, 'post__not_in' => array( get_the_ID() ), 'category__in' => $category_ids, 'orderby' => 'date', 'order' => 'DESC' ); $query = new WP_Query( $args ); if ( $query->have_posts() ) { echo '<h3>最新文章</h3>'; echo '<ul>'; while ( $query->have_posts() ) { $query->the_post(); echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>'; } echo '</ul>'; } wp_reset_postdata(); ?>
本文链接:http://so.lmcjl.com/news/5499/