WordPress 开发带缩略图的小工具最新文章
在我们的wordpress边栏想显示我们发布的最新文章,并且带上缩略图,很多插件能帮我们完成这个功能,其实我们完全可以自己DIY,和wordpress原生态的小工具一样,直接拖过去就OK了,这里把流程和代码分享一下,有需要的朋友可以自己二次开发。本站的左边兰的最新文章就是采用了这个方法。 后台-外观-小工具-效果如下图: 开始之前你需要了解 widget 函数如何创建自定义侧边栏小工具,本站有篇文章详细介绍了用法和实例,wordpress主题开发创建你喜欢的小工具 流程:一、主题根目录下创建recent-posts.php 二、在functions.php文件中导入recent-posts.php,这样做的目的是不让functions.php太臃肿,独立出来好管理。 三、根据你的主题样式,在style.css定义你的前端显示样式,为了你方便修改样式表,我这里加了类。 recent-posts.php源码点击查看recent-posts.php源码 /** ?* 带缩略图的最新文章小工具 ?* ?* web:www.511yj.com ?*/ class yj_Recent_Posts extends WP_Widget { public function __construct() { parent::__construct( 'yj_rp',// Base ID __('最新文章','yj'),// Name array( 'description' => __( '显示你发布的最新文章并且带有缩略图.','yj' ),) // Args ); } public function widget( $args,$instance ) { if (isset($instance['title'])) : $title = apply_filters( 'widget_title',$instance['title'] ); $no_of_posts = apply_filters( 'no_of_posts',$instance['no_of_posts'] ); else : $title = __('Latest Posts','yj'); $no_of_posts = 5; endif; echo $args['before_widget']; if ( ! empty( $title ) ) echo $args['before_title'] . $title . $args['after_title']; // WP_Query arguments $qa = array ( 'post_type' => 'post', 'posts_per_page' => $no_of_posts, 'offset' => 0, 'ignore_sticky_posts' => 1 ); // The Query $recent_articles = new WP_Query( $qa ); if($recent_articles->have_posts()) : ?>
while($recent_articles->have_posts()) : $recent_articles->the_post(); ?> |