WordPress专题 ·

WordPress针对360智能摘要、百度星火计划目录分类显示不同效果,并显示附件大小

功能:实现WordPress针对360智能摘要百度星火计划目录分类显示不同效果,比如软件下载页面显示软件下载类智能摘要,文章类显示文章的智能摘要,并可以显示非图片(应用类)附件大小

在主题下functions.php中填写以下代码即可,效果如下图所示:

WordPress针对360智能摘要、百度星火计划目录分类显示不同效果,并显示附件大小 WordPress专题 第1张

/**
* WordPress针对360搜索智能摘要的优化措施
* 百度星火、360搜索智能摘要同时满足版
* www.amingstudio.com
*/
add_action('wp_head', 'starfire',0);
if(!function_exists('starfire')){
  function starfire(){
    //新增判断,如果是原创文章才加入星火计划META申明
    $copy = get_post_meta($post->ID, 'author', true);
    if (is_single()) {
        date_default_timezone_set('PRC');
    //360智能摘要,百度星火计划
    //如果页面为soft软件下载类
    if (in_category(array('win-do','win','components'))){
        echo '<meta property="og:type" content="soft"/>';
        echo '<meta property="og:description" content="'.get_mypost_excerpt($post->ID, 220).'……" />';
        echo '<meta property="og:soft:file_size" content="'.wpatt_filsize().'"/>';
        echo '<meta property="og:soft:operating_system" content="Winxp/vista/win7/win8/2000/2003"/>';
        echo '<meta property="og:image" content="'.get_mypost_thumbnail($post->ID).'" />';
        echo '<meta property="og:release_date" content="'.get_the_date('c').'"/>';
        echo '<meta property="og:title" content="';
        single_post_title();
        echo '_';
        bloginfo('name'); 
        echo '" />';
        echo '<meta property="og:soft:language" content="简体中文"/>';
        echo '<meta property="og:soft:license" content="免费软件"/>';
    echo '<meta property="og:soft:url" content="'.get_permalink().'"/>';}
else if (in_category(array('mob-do','app'))){
        echo '<meta property="og:type" content="soft"/>';
        echo '<meta property="og:description" content="'.get_mypost_excerpt($post->ID, 220).'……" />';
        echo '<meta property="og:soft:file_size" content="'.wpatt_filsize().'"/>';
        echo '<meta property="og:soft:operating_system" content="Android/IOS"/>';
        echo '<meta property="og:image" content="'.get_mypost_thumbnail($post->ID).'" />';
        echo '<meta property="og:release_date" content="'.get_the_date('c').'"/>';
        echo '<meta property="og:title" content="';
        single_post_title();
        echo '_';
        bloginfo('name'); 
        echo '" />';
        echo '<meta property="og:soft:language" content="简体中文"/>';
        echo '<meta property="og:soft:license" content="免费软件"/>';
    echo '<meta property="og:soft:url" content="'.get_permalink().'"/>';}
    //如果页面为article文章类
else{
        echo '<meta property="og:type" content="article"/>';
        echo '<meta property="og:image" content="'.get_mypost_thumbnail($post->ID).'" />';
        echo '<meta property="og:release_date" content="'.get_the_date('c').'"/>';
        echo '<meta property="article:published_time" content="'.get_the_date('c').'"/>';
        echo '<meta property="og:title" content="';
        single_post_title();
        echo '_';
        bloginfo('name'); 
        echo '" />';
        echo '<meta property="og:description" content="'.get_mypost_excerpt($post->ID, 220).'……" />';
        echo '<meta property="article:author" content="';
        bloginfo('name');
        echo '" />';
        echo '<meta property="article:published_first" content="';
        bloginfo('name');
        echo ',';
        the_permalink();
        echo '" />';
       }
    }
    }
  }
/**
* WordPress 获取文章摘要整理版
*/
function get_mypost_excerpt($post_ID,$len){
    if (!function_exists('utf8Substr')) {
        function utf8Substr($str, $from, $len) {
            return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$from.'}'.
                '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s',
                '$1',$str);
        }
    }
    if(!$post_content){
            $post = get_post($post_ID);
            $post_content = $post->post_content;
   }
    if ($post->post_excerpt) {
            $description  = $post->post_excerpt;
    } else {
        if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){
            $post_content = $result['1'];
        } else {
            $post_content_r = explode("\n",trim(strip_tags($post->post_content)));
            $post_content = $post_content_r['0'];
        }
        $description = utf8Substr($post_content,0,$len);
        return $description;
    }
}
/**
* WordPress 获取文章图片加强版
*/
function get_mypost_thumbnail($post_ID){
    if (has_post_thumbnail()) {
            $timthumb_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_ID), 'full' );
            $url = $timthumb_src[0];
    } else {
        if(!$post_content){
            $post = get_post($post_ID);
            $post_content = $post->post_content;
        }
        preg_match_all('|<img.*?src=[\'"](.*?)[\'"].*?>|i', do_shortcode($post_content), $matches);
        if( $matches && isset($matches[1]) && isset($matches[1][0]) ){
            $url =  $matches[1][0];
        }else{
            $url =  '';
        }
    }
    return $url;
}
//格式化文件字节
function formatSizeUnits($bytes)
{
    if ($bytes >= 1073741824)
    {
        $bytes = number_format($bytes / 1073741824, 2) . ' GB';
    }
    elseif ($bytes >= 1048576)
    {
        $bytes = number_format($bytes / 1048576, 2) . ' MB';
    }
    elseif ($bytes >= 1024)
    {
        $bytes = number_format($bytes / 1024, 2) . ' KB';
    }
    elseif ($bytes > 1)
    {
        $bytes = $bytes . ' bytes';
    }
    elseif ($bytes == 1)
    {
        $bytes = $bytes . ' byte';
    }
    else
    {
        $bytes = '0 bytes';
    }
    return $bytes;
}
//获取附件大小www.amingstudio.com
function wpatt_filsize() {
 $attachments = get_attached_media( 'application', $post->ID );
if ($attachments){
foreach ($attachments as $attachment){
if ((file_exists(get_attached_file($attachment->ID)))) {
                $fsize = formatSizeUnits(filesize(get_attached_file($attachment->ID)));
            } else {
                $fsize = '错误';
            }
}}
return $fsize;
}

参与评论