最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
wordpress中AJAX随机取一条记录的例子
时间:2014-03-17 编辑:简简单单 来源:一聚教程网
点击“换一个”随机换一条内容。
由于有“标准”“图像”“视频”等文章形式,这里换一个需要随机获取一篇文章形式是图像的文章,并且把图片,和链接取出,使用ajax替换内容。
①我们可以建立一个新页面模板,模板名字就叫做recommend
新建页面模板:
/*
* Template Name:换一条
*/
然后在后台管理新建一个页面,选择这个模板,取名recommend,这样就建了一个新页面
②在模板中取得并输出需要替换的内容
代码如下 | 复制代码 |
/* * Template Name:换一条 */ www.111com.net $args = array( 'tax_query' => array( array( 'showposts' => '1', 'taxonomy' => 'post_format', 'field' => 'slug', 'terms' => 'post-format-image', ) ), 'orderby'=>'rand', ); query_posts($args); the_post(); ?> |
这是随机找出一篇文章形式为图像的文章。
代码如下 | 复制代码 |
catch_that_image();?> |
这里就是需要显示的html,我们把链接和图片地址都替换了。
catch_that_image函数:
代码如下 | 复制代码 |
function catch_that_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/ $first_img = $matches [1] [0]; // no image found display default image instead if(empty($first_img)){ $first_img = "/images/default.jpg"; } return $first_img; } ?> |
这个函数可以取出改篇文章的第一篇图片。
③在前端页面写上ajax函数
代码如下 | 复制代码 |
|
这样,这个“换一个”ajax效果就做好了。
相关文章
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31
- 使用HTML截图并保存为本地图片的代码展示 10-31