<?php

//注册菜单   
if( function_exists('register_nav_menus') ){   
    register_nav_menus(   
        array(   
            'primary' => __( '主导航菜单' ),   
        )   
    );   
}  

//文章缩略图获取
 function dm_the_thumbnail() {  
     global $post;  
     if ( has_post_thumbnail() ) {  
         echo '<a href="'.get_permalink().'" class="pic">'; 
         $domsxe = simplexml_load_string(get_the_post_thumbnail());
         $thumbnailsrc = $domsxe->attributes()->src;  
         echo '<img src="'.$thumbnailsrc.'" alt="'.trim(strip_tags( $post->post_title )).'" />';
         echo '</a>';  
     } else {
         $content = $post->post_content;  
         preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);  
         $n = count($strResult[1]); 
         $random = mt_rand(1, 5);
         if($n > 0){
             echo '<a href="'.get_permalink().'" class="pic"><img src="'.$strResult[1][0].'" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }else {
             echo '<a href="'.get_permalink().'" class="pic"><img src="'.get_bloginfo('template_url').'/images/random/longtian'.$random.'.jpg" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }  
     }  
 }
 add_theme_support('post-thumbnails');
 set_post_thumbnail_size(140, 104, true);

 //分类业文章缩略图
   function dm_the_thumbnail1() {  
     global $post;  
     if ( has_post_thumbnail() ) {  
         echo '<a href="'.get_permalink().'" class="pic">'; 
         $domsxe = simplexml_load_string(get_the_post_thumbnail());
         $thumbnailsrc = $domsxe->attributes()->src;  
         echo '<img src="'.$thumbnailsrc.'" alt="'.trim(strip_tags( $post->post_title )).'" />';
         echo '</a>';  
     } else {
         $content = $post->post_content;  
         preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);  
         $n = count($strResult[1]); 
         $random = mt_rand(1, 5);
         if($n > 0){
             echo '<a href="'.get_permalink().'" class="pic"><img src="'.$strResult[1][0].'" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }else {
             echo '<a href="'.get_permalink().'" class="pic"><img src="'.get_bloginfo('template_url').'/images/random/longtian'.$random.'.jpg" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }  
     }  
 }
 add_theme_support('post-thumbnails');
 set_post_thumbnail_size(254, 164, true);
//首页侧栏缩略图
   function dm_the_thumbnail2() {  
     global $post;  
     if ( has_post_thumbnail() ) {  
         echo '<a href="'.get_permalink().'" class="pic">'; 
         $domsxe = simplexml_load_string(get_the_post_thumbnail());
         $thumbnailsrc = $domsxe->attributes()->src;  
         echo '<img src="'.$thumbnailsrc.'" alt="'.trim(strip_tags( $post->post_title )).'" />';
         echo '</a>';  
     } else {
         $content = $post->post_content;  
         preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);  
         $n = count($strResult[1]); 
         $random = mt_rand(1, 5);
         if($n > 0){
             echo '<a href="'.get_permalink().'" class="pic"><img width="60" height="60" src="'.$strResult[1][0].'" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }else {
             echo '<a href="'.get_permalink().'" class="pic"><img width="60" height="60" src="'.get_bloginfo('template_url').'/images/random/longtian'.$random.'.jpg" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }  
     }  
 }
 add_theme_support('post-thumbnails');
 set_post_thumbnail_size(60, 60, true);
 //侧栏焦点关注缩略图
   function dm_the_thumbnail3() {  
     global $post;  
     if ( has_post_thumbnail() ) {  
         echo '<a href="'.get_permalink().'" class="pic">'; 
         $domsxe = simplexml_load_string(get_the_post_thumbnail());
         $thumbnailsrc = $domsxe->attributes()->src;  
         echo '<img src="'.$thumbnailsrc.'" alt="'.trim(strip_tags( $post->post_title )).'" />';
         echo '</a>';  
     } else {
         $content = $post->post_content;  
         preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);  
         $n = count($strResult[1]); 
         $random = mt_rand(1, 5);
         if($n > 0){
             echo '<a href="'.get_permalink().'" class="pic"><img width="40" height="40" src="'.$strResult[1][0].'" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }else {
             echo '<a href="'.get_permalink().'" class="pic"><img width="40" height="40" src="'.get_bloginfo('template_url').'/images/random/longtian'.$random.'.jpg" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }  
     }  
 }
 add_theme_support('post-thumbnails');
 set_post_thumbnail_size(40, 40, true);
 //文章热卖推荐缩略图
   function dm_the_thumbnail4() {  
     global $post;  
     if ( has_post_thumbnail() ) {  
         echo '<a href="'.get_permalink().'" class="pic">'; 
         $domsxe = simplexml_load_string(get_the_post_thumbnail());
         $thumbnailsrc = $domsxe->attributes()->src;  
         echo '<img src="'.$thumbnailsrc.'" alt="'.trim(strip_tags( $post->post_title )).'" />';
         echo '</a>';  
     } else {
         $content = $post->post_content;  
         preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);  
         $n = count($strResult[1]); 
         $random = mt_rand(1, 5);
         if($n > 0){
             echo '<a href="'.get_permalink().'" class="pic"><img border="0" width="65" height="65" src="'.$strResult[1][0].'" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }else {
             echo '<a href="'.get_permalink().'" class="pic"><img border="0" width="65" height="65" src="'.get_bloginfo('template_url').'/images/random/longtian'.$random.'.jpg" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }  
     }  
 }
 add_theme_support('post-thumbnails');
 set_post_thumbnail_size(60, 65, true);
 //侧分类随机文章缩略图
   function dm_the_thumbnail5() {  
     global $post;  
     if ( has_post_thumbnail() ) {  
         echo '<a href="'.get_permalink().'" class="pic">'; 
         $domsxe = simplexml_load_string(get_the_post_thumbnail());
         $thumbnailsrc = $domsxe->attributes()->src;  
         echo '<img src="'.$thumbnailsrc.'" alt="'.trim(strip_tags( $post->post_title )).'" />';
         echo '</a>';  
     } else {
         $content = $post->post_content;  
         preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);  
         $n = count($strResult[1]); 
         $random = mt_rand(1, 5);
         if($n > 0){
             echo '<a href="'.get_permalink().'" class="pic"><img border="0" width="75" height="75" src="'.$strResult[1][0].'" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }else {
             echo '<a href="'.get_permalink().'" class="pic"><img border="0" width="75" height="75" src="'.get_bloginfo('template_url').'/images/random/longtian'.$random.'.jpg" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }  
     }  
 }
 add_theme_support('post-thumbnails');
 set_post_thumbnail_size(75, 75, true);
 //tag侧栏缩略图
   function dm_the_thumbnail6() {  
     global $post;  
     if ( has_post_thumbnail() ) {  
         echo '<a href="'.get_permalink().'" class="pic">'; 
         $domsxe = simplexml_load_string(get_the_post_thumbnail());
         $thumbnailsrc = $domsxe->attributes()->src;  
         echo '<img src="'.$thumbnailsrc.'" alt="'.trim(strip_tags( $post->post_title )).'" />';
         echo '</a>';  
     } else {
         $content = $post->post_content;  
         preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);  
         $n = count($strResult[1]); 
         $random = mt_rand(1, 5);
         if($n > 0){
             echo '<a href="'.get_permalink().'" class="pic"><img width="90" height="90" src="'.$strResult[1][0].'" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }else {
             echo '<a href="'.get_permalink().'" class="pic"><img width="90" height="90" src="'.get_bloginfo('template_url').'/images/random/longtian'.$random.'.jpg" alt="'.get_the_title().'" title="'.get_the_title().'"/></a>';  
         }  
     }  
 }
 add_theme_support('post-thumbnails');
 set_post_thumbnail_size(90, 90, true);
//文章浏览次数    
function get_post_views ($post_id) {   
        $count_key = 'views';   
        $count = get_post_meta($post_id, $count_key, true);   
      
        if ($count == '') {   
            delete_post_meta($post_id, $count_key);   
            add_post_meta($post_id, $count_key, '0');   
            $count = '0';   
            } 
        echo number_format_i18n($count);   
    }    
    function set_post_views () {   
        global $post;   
        $post_id = $post->ID;   
        $count_key = 'views';   
        $count = get_post_meta($post_id, $count_key, true);   
        if (is_single() || is_page()) {   
            if ($count == '') {   
                delete_post_meta($post_id, $count_key);   
                add_post_meta($post_id, $count_key, '0');   
            } else {   
                update_post_meta($post_id, $count_key, $count + 1);   
            }   
        }   
    }   
    add_action('get_header', 'set_post_views');  
//调用浏览次数最多的文章
 function get_timespan_most_viewed($mode = '', $limit = 10, $days = 30, $display = true) {   
    global $wpdb, $post;       
    $limit_date = current_time('timestamp') - ($days*86400);    
    $limit_date = date("Y-m-d H:i:s",$limit_date);     
    $where = '';   
    $temp = '';   
    if(!empty($mode) && $mode != 'both') {   
        $where = "post_type = '$mode'";   
    } else {   
        $where = '1=1';   
    }   
    $most_viewed = $wpdb->get_results("SELECT $wpdb->posts.*, (meta_value+0) AS views FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON $wpdb->postmeta.post_id = $wpdb->posts.ID WHERE post_date < '".current_time('mysql')."' AND post_date > '".$limit_date."' AND $where AND post_status = 'publish' AND meta_key = 'views' AND post_password = '' ORDER  BY views DESC LIMIT $limit");   
    if($most_viewed) {   
        foreach ($most_viewed as $post) {   
            $post_title = get_the_title();   
            $post_views = intval($post->views);   
            $post_views = number_format($post_views);   
            $temp .= "<li><span><a rel=\"bookmark\" title=\"$post_title - $limit_date\"href=\"".get_permalink()."\">$post_title</a></span>"."</li>";
        }   
    } else {   
        $temp = '<li>'.get_post_views($post -> ID).'</li>'."\n";   
    }   
    if($display) { 
        echo $temp;   
    } else {   
        return $temp;   
    }   
}  
//实现分页
function pagination($query_string){   
global $posts_per_page, $paged;   
$my_query = new WP_Query($query_string ."&posts_per_page=-1");   
$total_posts = $my_query->post_count;   
if(empty($paged))$paged = 1;   
$prev = $paged - 1;   
$next = $paged + 1;   
$range = 2; // only edit this if you want to show more page-links   
$showitems = ($range * 2)+1;   
  
$pages = ceil($total_posts/$posts_per_page);   
if(1 != $pages){   
echo "<div class='pagination'>";   
echo ($paged > 2 && $paged+$range+1 > $pages && $showitems < $pages)? "<a href='".get_pagenum_link(1)."'>最前</a>":"";   
echo ($paged > 1 && $showitems < $pages)? "<a href='".get_pagenum_link($prev)."'>上一页</a>":"";   
  
for ($i=1; $i <= $pages; $i++){   
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){   
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";   
}   
}   
  
echo ($paged < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($next)."'>下一页</a>" :"";   
echo ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) ? "<a href='".get_pagenum_link($pages)."'>最后</a>":"";   
echo "</div>\n";   
}   
}  


//面包屑导航
function wheatv_breadcrumbs() {
  $delimiter = ' > ';
  $name = '首页'; //
 
  if ( !is_home() ||!is_front_page() || is_paged() ) {
 
    global $post;
    $home = get_bloginfo('url');
    echo '<a href="' . $home . '"  class="gray">' . $name . '</a> ' . $delimiter . ' ';
 
    if ( is_category() ) {
      global $wp_query;
      $cat_obj = $wp_query->get_queried_object();
      $thisCat = $cat_obj->term_id;
      $thisCat = get_category($thisCat);
      $parentCat = get_category($thisCat->parent);
      if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
      echo single_cat_title();
 
    } elseif ( is_day() ) {
      echo '<a href="' . get_year_link(get_the_time('Y')) . '"  class="gray">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
      echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '"  class="gray">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
      echo get_the_time('d');
 
    } elseif ( is_month() ) {
      echo '<a href="' . get_year_link(get_the_time('Y')) . '"  class="gray">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
      echo get_the_time('F');
 
    } elseif ( is_year() ) {
      echo get_the_time('Y');
 
    } elseif ( is_single() ) {
      $cat = get_the_category(); $cat = $cat[0];
      echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
      echo "正文";
 
    } elseif ( is_page()||!$post->post_parent ) {
      the_title();
 
    } elseif ( is_page()||$post->post_parent ) {
      $parent_id  = $post->post_parent;
      $breadcrumbs = array();
      while ($parent_id) {
        $page = get_page($parent_id);
        $breadcrumbs[] = '<a href="http://www.wheatv.com/site/wp-admin/ . get_permalink($page->ID) . "  class="gray">' . get_the_title($page->ID) . '</a>';
        $parent_id  = $page->post_parent;
      }
      $breadcrumbs = array_reverse($breadcrumbs);
      foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
      the_title();
 
    } elseif ( is_search() ) {
      echo get_search_query();
 
    } elseif ( is_tag() ) {
      echo single_tag_title();
 
    } elseif ( is_author() ) {
       global $author;
      $userdata = get_userdata($author);
      echo '由'.$userdata->display_name.'发表';
 
    } elseif ( is_404() ) {
      echo '404 错误';
    }
 
    if ( get_query_var('paged') ) {
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
      echo '第' . ' ' . get_query_var('paged').' 页';
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
    }
  }else{
    echo $name;
  }
}

//友情链接
add_filter('pre_option_link_manager_enabled', '__return_true');
//随机文章
function random_posts($posts_num=5,$before='<li>',$after='</li>'){
    global $wpdb;
    $sql = "SELECT ID, post_title,guid
            FROM $wpdb->posts
            WHERE post_status = 'publish' ";
    $sql .= "AND post_title != '' ";
    $sql .= "AND post_password ='' ";
    $sql .= "AND post_type = 'post' ";
    $sql .= "ORDER BY RAND() LIMIT 0 , $posts_num ";
    $randposts = $wpdb->get_results($sql);
    $output = '';
    foreach ($randposts as $randpost) {
        $post_title = stripslashes($randpost->post_title);
        $permalink = get_permalink($randpost->ID);
        $output .= $before.'<a href="'
            . $permalink . '"  rel="bookmark" title="';
        $output .= $post_title . '">' . $post_title . '</a>';
        $output .= $after;
    }
    echo $output;
}
//调用指定分类文章
function newArticle($num,$cat){

    $args=array(

        'posts_per_page' => $num,

        'cat' => $cat,

        'order' => 'desc'

    );

    $posts = query_posts($args);

    if( have_posts() ) :

        $html = '<div class="newArticle">';

        $html .= '<ul>';

        foreach($posts as $post) :

            $html .= '<li>';

            $html .= '<a href="'.get_permalink($post->ID).'" rel="bookmark" title="'.$post->post_title.'">'.$post->post_title.'</a>';

            $html .= '</li>';

        endforeach;

        $html .= '</ul>';

        $html .= '</div>';

    endif;

    echo $html;

}
//增强编辑器开始
function add_editor_buttons($buttons) {
$buttons[] = 'fontselect';
$buttons[] = 'fontsizeselect';
$buttons[] = 'backcolor';
$buttons[] = 'underline';
$buttons[] = 'hr';
$buttons[] = 'sub';
$buttons[] = 'sup';
$buttons[] = 'cut';
$buttons[] = 'copy';
$buttons[] = 'paste';
$buttons[] = 'cleanup';
$buttons[] = 'wp_page';
$buttons[] = 'newdocument';
return $buttons;
}
add_filter("mce_buttons_3", "add_editor_buttons");
//增强编辑器结束
//Gzip压缩   
Function Dtheme_gzip() {   
If ( Strstr($_SERVER['REQUEST_URI'], ‘/Js/Tinymce’) )   
Return False;   
If ( ( Ini_get(‘Zlib.Output_compression’) == ‘On’ || Ini_get(‘Zlib.Output_compression_level’) > 0 ) || Ini_get(‘Output_handler’) == ‘Ob_gzhandler’ )   
Return False;   
If (Extension_loaded(‘Zlib’) && !Ob_start(‘Ob_gzhandler’))   
Ob_start();   
}   
Add_action(‘Init’,'Dtheme_gzip’); 
?>
