wordpress文章图片点击下一页

wordpress文章图片点击下一页

作者:明星 |  时间:2017-06-03 |  浏览:2933 |  评论已关闭 条评论

使用wordpress建站,如果发布的图文内容较长需要分页,用户每次都只能点击下一页或页码进行翻页阅读,这样小小的阅读障碍容易造成用户反感,如果能和大多数图片站一样,给图片添加一个翻页的链接,人均PV便可倍数增长。

把以下代码丢进wordpress主题的functions.php,即可实现给图片自动加上下一分页的链接并自动添加alt信息。

//获取下一页链接
function nextpage(){
    global $pages;
    $link = get_permalink();
    $max_page = count($pages);
    if (get_query_var('page')) {
    $pageno =  get_query_var('page');
    }
    else{$pageno = '1';}
    $next = $pageno+'1';
    if ($pageno == $max_page) {
    $nextpage = get_permalink(get_adjacent_post(true,'',true)); 
    }
    else{
        $nextpage = $link.'/'.$next;
    }
    return  $nextpage;
}
function img_info ($img_info){
    //$pattern = "/<img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)alt=('|\")(.*?)('|\")(.*?)>/i";
    $pattern = "/<img(.*?)src=('|\")([^>]*).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";
    $replacement = '<a href="'.nextpage().'" title="点击图片查看下一张" ><img$1src=$2$3.$4$5 alt="'.get_the_title().'" $10></a>';
    $img_info = preg_replace($pattern, $replacement, $img_info);
return $img_info;
}
add_filter('the_content', 'img_info');
标签:

相关推荐
更多

评论已关闭。