WordPress 开发带缩略图随机文章小工具

鱼见海资源网最新教程:

为了让你的随机文章看起来更吸睛,这里加上特色图像的缩略图,效果如本站左边栏的随机文章,步骤和代码如下:

后台-外观-小工具-效果如下图:

流程:

一、主题根目录下创建rand-posts.php

二、在functions.php文件中导入rand-posts.php,这样做的目的是不让functions.php太臃肿,独立出来好管理。

/**
 * 带缩略图的随机文章
 */
require get_template_directory() . '/inc/rand-posts.php';

三、根据你的主题样式,在style.css定义你的前端显示样式,为了你方便修改样式表,我这里给 li 加了类class=’rp-thumb’。

 __( 'yj-带缩略图的随机文章小工具.', '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,
			'orderby' =>'rand'
		);		
		// The Query
		$recent_articles = new WP_Query( $qa );
		if($recent_articles->have_posts()) : ?>
		
    have_posts()) : $recent_articles->the_post(); ?>
  • Oops, there are no posts.

文章来源于互联网

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容