wordpress中需要用到很多方法,他们都是放在主题的functions.php文件中的,下面就来展示一些常用的wordpress代码:
用户浏览文章,文章浏览器+1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| <?php
//浏览量设置
//获取浏览数-参数文章ID
function getPostViews($postID) {
//字段名称
$count_key = 'post_views_count';
//获取字段值即浏览次数
$count = get_post_meta($postID, $count_key, true);
//如果为空设置为0
if ($count == '') {
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
//设置浏览数-参数文章ID
function setPostViews($postID) {
//字段名称
$count_key = 'post_views_count';
//先获取获取字段值即浏览次数
$count = get_post_meta($postID, $count_key, true);
//如果为空就设为0
if ($count == '') {
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
} else {
//如果不为空,加1,更新数据
$count++;
update_post_meta($postID, $count_key, $count);
}
}
//浏览量设置
?> |
获取文章第一张缩略图
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| // 获取文章第一张缩略图
function catch_that_image() {
global $post;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img*.+src=[\'"]([^\'"]+)[\'"].*>/iU', wp_unslash($post->post_content), $matches);
if(empty($output)){
$first_img = "/wp-content/themes/h/images/default.png";
}else {
$first_img = $matches [1][0];
}
return $first_img;
} |
「梦想一旦被付诸行动,就会变得神圣,如果觉得我的文章对您有用,请帮助本站成长」
共 0 条评论关于"wordpress代码常用函数整理"
最新评论