Advertisement
Guest User

Category calendar php

a guest
Jun 13th, 2012
860
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.98 KB | None | 0 0
  1. <?php
  2. $_SERVER[ 'HTTP_HOST' ] = 'localhost';
  3. $wp_load_loc = "../../../wp-load.php";
  4. require_once($wp_load_loc);          
  5. global $post;  
  6. global $wpdb;
  7.  
  8.     function get_calendar_by_category($cat, $current_year = null, $current_month = null){
  9.         global $post;  
  10.         global $wpdb;
  11.        
  12.         $days_in_month = cal_days_in_month(CAL_GREGORIAN, date("n"), date("Y"));  
  13.          
  14.         if(!$current_year){$current_year = date("Y");}else{
  15.             $days_in_month = cal_days_in_month(CAL_GREGORIAN, $current_month, $current_year);
  16.         }
  17.         if(!$current_month){$current_month = date("n");}else{
  18.             $days_in_month = cal_days_in_month(CAL_GREGORIAN, $current_month, $current_year);
  19.         }
  20.        
  21.         if($current_month == 1){
  22.             $prev_month = 12;
  23.             $prev_year = ($current_year - 1);
  24.         }else{
  25.             $prev_month = ($current_month - 1);
  26.             $prev_year = $current_year;
  27.         }
  28.        
  29.         if($current_month == 12){
  30.             $next_month = 1;
  31.             $next_year = ($current_year + 1);
  32.         }else{
  33.             $next_month = ($current_month + 1);
  34.             $next_year = $current_year;
  35.         }
  36.                                
  37.         $args = array(        
  38.             'cat' => $cat,
  39.             'posts_per_page' => -1,
  40.             'post_type' => 'post',
  41.             'post_status' => 'publish',
  42.             'year' => $prev_year,
  43.             'monthnum' => $prev_month
  44.         );
  45.        
  46.         $check_prev_month_for_posts = query_posts( $args );    
  47.         $check_prev_month_for_posts_count = count($check_prev_month_for_posts);
  48.        
  49.         if($check_prev_month_for_posts_count){                                                    
  50.             $prev_button = "<div id = 'calendar_toggle_prev' onclick = 'get_calendar(" . $prev_year . ", " . $prev_month . ");'><<</div>";    
  51.         }else{
  52.             $prev_button = "";
  53.         }                
  54.                
  55.         wp_reset_query();  
  56.                  
  57.         $args = array(        
  58.             'cat' => $cat,
  59.             'posts_per_page' => -1,
  60.             'post_type' => 'post',
  61.             'post_status' => 'publish',
  62.             'year' => $next_year,
  63.             'monthnum' => $next_month
  64.         );
  65.        
  66.         $check_next_month_for_posts = query_posts( $args );  
  67.         $check_next_month_for_posts_count = count($check_next_month_for_posts);
  68.        
  69.         if($check_next_month_for_posts_count){
  70.             $next_button = "<div id = 'calendar_toggle_next' onclick = 'get_calendar(" . $next_year . ", " . $next_month . ");'>>></div>";
  71.         }else{
  72.             $next_button = "";
  73.         }
  74.        
  75.         wp_reset_query();  
  76.        
  77.         $calendar = "";
  78.         $calendar .= "<table class = 'category_calendar'>";
  79.         $calendar .= "<thead><tr><td colspan = '7'><span class = 'underline_this'>" . $prev_button . $next_button . date( 'F', mktime(0, 0, 0, $current_month) ) . ' - ' . $current_year . "</span></td></tr></thead>";
  80.         $calendar .= "<tbody><tr>";
  81.            
  82.             for($a = 0; $a <= 34; $a++){
  83.                 if($a % 7 == 0){
  84.                     $next_row = true;    
  85.                 }                  
  86.                
  87.                 if($next_row){
  88.                     $calendar .= "</tr><tr>";    
  89.                 }      
  90.                        
  91.                     $calendar .= "<td>";
  92.                             if($a != 0 && $a <= $days_in_month){
  93.                                
  94.                                 $args = array(        
  95.                                     'cat' => $cat,
  96.                                     'posts_per_page' => -1,
  97.                                     'post_type' => 'post',
  98.                                     'post_status' => 'publish',
  99.                                     'year' => $current_year,
  100.                                     'monthnum' => str_pad($current_month, 2, "0", STR_PAD_LEFT),
  101.                                     'day' => str_pad($a, 2, "0", STR_PAD_LEFT)
  102.                                 );
  103.                                
  104.                                 $check_day_for_posts = query_posts( $args );
  105.                                
  106.                                 $title_tag = "";
  107.                                
  108.                                 foreach( $check_day_for_posts as $post ) :  setup_postdata($post);
  109.                                      $title_tag .= htmlspecialchars(get_the_title(), ENT_QUOTES) . ", ";
  110.                                 endforeach;    
  111.                                
  112.                                 if($title_tag){
  113.                                     $styling = "class = 'active_day'";
  114.                                     $link = "<a $styling title = '" . $title_tag . "' href = '" . get_bloginfo('url') . "/" . $current_year . "/" . str_pad($current_month, 2, "0", STR_PAD_LEFT) . "/" . str_pad($a, 2, "0", STR_PAD_LEFT) . "/'>";
  115.                                     $end = "</a>";
  116.                                 }else{
  117.                                     $styling = "";
  118.                                     $link = "";
  119.                                     $end = "";
  120.                                 }
  121.                                
  122.                                 $calendar .= $link . "<span>" . $a . "</span>" . $end;
  123.                                
  124.        
  125.                                 wp_reset_query();
  126.                             }
  127.                         $calendar .= "</td>";
  128.                    
  129.                 $next_row = false;  
  130.             }
  131.            
  132.         $calendar .= "</tbody></tr></table>";
  133.        
  134.         return $calendar;
  135.     }  
  136.    
  137.     if($_GET["the_year"]){
  138.         $the_year = $_GET["the_year"];
  139.     }else{
  140.         $the_year = null;
  141.     }  
  142.    
  143.     if($_GET["the_month"]){
  144.         $the_month = $_GET["the_month"];
  145.     }else{
  146.         $the_month = null;
  147.     }
  148.     echo get_calendar_by_category(8, $the_year, $the_month);
  149. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement