用wordpress制作网站模板时,可以使用以下代码,即可实现按栏目别名调用该栏目下的最新内容。

<?php $cmntCnt = 1;
$cat=get_category_by_slug('news'); 
?>
<?php $posts = get_posts( "category=$cat->term_id&numberposts=3" ); ?>
<?php if( $posts ) : ?>
<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
<li>  
<h6 class="wow fadeInUp" data-wow-delay="0.2s"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php echo cut_str($post->post_title,38); ?></a></h6><span class="wow fadeInUp" data-wow-delay="0.4s"><?php the_category(', '); ?></span><span class="text-muted wow fadeInUp" data-wow-delay="0.4s"> - <?php the_time(get_option('date_format')) ?></span>
<p class="text-muted d-block mt-3 wow fadeInUp" data-wow-delay="0.6s"><?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 100, '…'); ?></p>
</li>

<?php endforeach; ?>
<?php endif; ?>
<?php wp_reset_query();?>

【免费WP主题QQ群:556319531】

代码片断