Advertisement
Guest User

Functions-Type-Project.php

a guest
Sep 26th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.13 KB | None | 0 0
  1. <?php
  2.  
  3. //add_theme_support( 'post-thumbnails', array( 'project'));
  4.  
  5. add_shortcode("projects-columns" , "ewf_projects_sc_columns");
  6. add_shortcode("projects-detailed" , "ewf_projects_sc_detailed");
  7. add_shortcode("projects-related" , "ewf_projects_sc_related");
  8. add_shortcode("projects-filter" , "ewf_projects_sc_filter");
  9.  
  10. add_action('init' , 'ewf_register_type_project');
  11. add_action('init' , 'ewf_register_project_taxonomies', 0 );
  12. add_action('admin_menu' , 'ewf_projects_meta_install');
  13. add_action('save_post' , 'ewf_projects_meta_update');
  14.  
  15. add_image_size( 'project-preview-small', 220, 300, true);
  16. add_image_size( 'project-preview-large', 460, 180, true);
  17.  
  18.  
  19. function ewf_projects_meta_install() {
  20. add_meta_box( 'ewf_projects_meta',__('Projects settings', EWF_SETUP_THEME_DOMAIN), 'ewf_projects_meta_source', 'project', 'normal', 'high' );
  21. }
  22.  
  23. function ewf_projects_meta_update() {
  24. global $post;
  25. update_post_meta($post->ID, "project_date_interval", $_POST["project_date_interval"]);
  26. }
  27.  
  28. function ewf_projects_meta_source() {
  29. global $post;
  30.  
  31. $custom = get_post_custom($post->ID);
  32.  
  33. $project_interval = $custom["project_date_interval"][0];
  34.  
  35. echo '
  36. <div style="padding-top:10px;">
  37. <label style="display:block;padding:2px;">'.__('Date interval', EWF_SETUP_THEME_DOMAIN).': </label><input style="width:220px;" name="project_date_interval" value="'.$project_interval.'">
  38. </div>';
  39. }
  40.  
  41.  
  42. function ewf_projects_sc_filter($atts, $content){
  43. extract(shortcode_atts(array(
  44. "url" => null,
  45. "title" => null,
  46. "full" => 0
  47. ), $atts));
  48.  
  49. if ($full == 0){ $full = get_option(EWF_SETUP_THNAME."_page_portfolio", 0); }
  50.  
  51. if ($full) {
  52. $full_page = get_post($full);
  53.  
  54. if ($url == null) { $url = get_permalink($full); }
  55. if ($title == null) { $title = $full_page->post_title; }
  56. }
  57.  
  58. $terms = get_terms ('service');
  59. $src = null;
  60.  
  61. $current_url = strtolower($_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
  62. $current_url = str_replace(array('http://', 'https://', 'page/2/', 'page/3/', 'page/4/', 'page/5/', 'page/6/', 'page/7/', 'page/8/', 'page/9/', 'page/10/', 'page/11/', 'page/12/', 'page/13/', 'page/14/', 'page/15/' ),'',strtolower($current_url));
  63.  
  64. $src .= '<ul class="side-nav">';
  65. if ($full) {
  66. $full_url = str_replace(array('http://', 'https://'),'',strtolower($url));
  67.  
  68. $extra = null;
  69. if ($current_url==$full_url){ $extra = "class='current' "; }
  70.  
  71. $src .= '<li '.$extra.'><a href="'.$url.'">'.$title.'</a></li>';
  72. }
  73.  
  74. foreach($terms as $key => $service){
  75. $item_permalink = get_term_link($service->slug, 'service');
  76. $item_url = str_replace(array('http://', 'https://'),'',strtolower($item_permalink));
  77.  
  78. $extra = null;
  79. if ($current_url==$item_url){ $extra = "class='current' "; }
  80.  
  81. $src .= '<li '.$extra.'><a href="'.$item_permalink.'">'.$service->name.'</a></li>';
  82. }
  83. $src .= '</ul>';
  84.  
  85. return $src;
  86. }
  87. function ewf_projects_sc_detailed($atts, $content){
  88. global $paged, $post;
  89.  
  90. extract(shortcode_atts(array(
  91. "items" => 4,
  92. "nav" => "true",
  93. "order" => "asc",
  94. "service" => null
  95. ), $atts));
  96.  
  97.  
  98. $paged = get_query_var('paged') ? get_query_var('paged') : 1;
  99. $query = array( 'post_type' => 'project', 'posts_per_page' => $items, 'paged' => $paged, 'order'=>$order, 'orderby'=>'ID' );
  100.  
  101. if ($service != null){
  102. $query['tax_query'] = array(
  103. array(
  104. 'taxonomy' => 'service',
  105. 'field' => 'slug',
  106. 'terms' => array( $service )
  107. ));
  108. }
  109.  
  110. $wp_query_project = new WP_Query($query);
  111.  
  112. $items_src = null;
  113. $items_count = 0;
  114.  
  115. while ($wp_query_project->have_posts()) : $wp_query_project->the_post();
  116. $items_count++;
  117. $image_id = get_post_thumbnail_id();
  118.  
  119. $image_small_preview = wp_get_attachment_image_src($image_id,'project-preview-large');
  120. $image_large = wp_get_attachment_image_src($image_id,'large');
  121.  
  122. $items_src .= '<div class="portfolio-item row fixed">
  123. <div class="col-460">';
  124.  
  125. if ($image_id){
  126. $items_src.= '<a href="'.$image_large[0].'" class="portfolio-item-preview" rel="prettyPhoto" title="'.get_the_excerpt().'"><img src="'.$image_small_preview[0].'" width="460" height="180" alt="'.get_the_title().'" /></a>';
  127. }else{
  128. $items_src.= ewf_message(__('There is no featured image, please add one!', EWF_SETUP_THEME_DOMAIN));
  129. }
  130.  
  131. $items_src.= '</div>
  132. <div class="col-220 last">
  133. <h3 class="project-title"><a href="'.get_permalink().'">'.get_the_title().'</a></h3>
  134. <p>'.get_the_excerpt().'</p>
  135. <p><a href="'.get_permalink().'">'.__('Read More',EWF_SETUP_THEME_DOMAIN).'</a></p>';
  136.  
  137. $items_src.= '</div>
  138. </div>';
  139.  
  140. if ($wp_query_project->post_count > $items_count){
  141. $items_src.= '<div class="hr"></div>';
  142. }
  143.  
  144. if ($wp_query_project->post_count == $items_count && $nav == "true"){
  145. $items_src.= '<div class="hr"></div>';
  146. }
  147.  
  148. endwhile;
  149.  
  150. wp_reset_query();
  151.  
  152. if ($nav == "true"){
  153. $items_src .= ewf_projects_pagination($items, $wp_query_project);
  154. }
  155.  
  156. return $items_src;
  157. }
  158.  
  159.  
  160. function ewf_projects_sc_related ($atts, $content){
  161. extract(shortcode_atts(array(
  162. "items" => 1,
  163. "exclude" => null,
  164. "order" => "asc",
  165. "id" => null,
  166. "service" => null,
  167. ), $atts));
  168.  
  169. global $post;
  170. $current_post = $post->ID;
  171. $include_posts = array();
  172.  
  173.  
  174. $items_count = 0;
  175. $items_row = 0;
  176. $items_src = null;
  177.  
  178. $query = array( 'post_type' => 'project', 'order'=> $order, 'orderby' => 'ID', 'posts_per_page'=>$items, 'tax_query' => array( array('taxonomy' => 'service', 'field' => 'slug', 'terms' => array( $service ))) );
  179.  
  180. if ($exclude != null){
  181. if (is_numeric($id)){
  182. // if we have only one ID
  183. $exclude_items[] = $id ;
  184. }else{
  185. //If there are more ids separated by comma
  186. $tmp_id = explode(',', trim($id));
  187. foreach($tmp_id as $key => $item_id){
  188. if (is_numeric($item_id)){
  189. $exclude_items[] = $item_id ;
  190. }
  191. }
  192. }
  193.  
  194. $exclude_items[] = $current_post;
  195. $query['post__not_in'] = $exclude_items;
  196. }else{
  197. $query['post__not_in'] = array($current_post);
  198. }
  199.  
  200.  
  201. if ($id != null){
  202. if (is_numeric($id)){
  203. // if we have only one ID
  204. $include_posts[] = $id ;
  205. }else{
  206. //If there are more ids separated by comma
  207. $tmp_id = explode(',', trim($id));
  208. foreach($tmp_id as $key => $item_id){
  209. if (is_numeric($item_id)){
  210. $include_posts[] = $item_id ;
  211. }
  212. }
  213. }
  214.  
  215. unset($query['post__not_in']);
  216. unset($query['tax_query']);
  217.  
  218. $query['post__in'] = $include_posts;
  219. $query['posts_per_page'] = count($include_posts);
  220. }
  221.  
  222. $wp_query_project = new WP_Query($query);
  223.  
  224. while ($wp_query_project->have_posts()) : $wp_query_project->the_post();
  225. global $post;
  226.  
  227. $items_count++;
  228. $items_row++;
  229.  
  230. $extra_class = null;
  231.  
  232. $image_id = get_post_thumbnail_id();
  233. $image_small_preview = wp_get_attachment_image_src($image_id,'project-preview-small');
  234. $image_large = wp_get_attachment_image_src($image_id,'large');
  235.  
  236. if ($items_row == 4){ $extra_class = "last"; }
  237. if ($items_row == 1){ $items_src .= '<div class="row fixed">'; }
  238.  
  239. $items_src .='<div class="portfolio-item col-220 '.$extra_class.'">
  240. <p>
  241. <a href="'.$image_large[0].'" class="portfolio-item-preview" rel="prettyPhoto[projects]" title="'.get_the_excerpt().'">
  242. <img src="'.$image_small_preview[0].'" width="220" height="120" alt="'.get_the_title().'" />
  243. </a>
  244. </p>
  245. <h4><a href="'.get_permalink().'">'.get_the_title().'</a></h4>
  246. <p>'.get_the_excerpt().'</p></div>';
  247.  
  248. if ($items_row == 4){
  249. $items_row = 0;
  250. $items_src .= '</div>';
  251. }else {
  252. if ($wp_query_project->post_count == $items_count){ $items_src .= '</div><!--forced row end-->'; }
  253. }
  254.  
  255.  
  256.  
  257. endwhile;
  258.  
  259.  
  260. return $items_src;
  261. }
  262.  
  263. function ewf_projects_sc_columns($atts, $content){
  264. extract(shortcode_atts(array(
  265. "items" => 6,
  266. "id" => null,
  267. "nav" => false,
  268. "order" => "asc",
  269. "link" => false,
  270. "title" => __("view more work", EWF_SETUP_THEME_DOMAIN),
  271. "url" => "#",
  272. "service" => null,
  273. ), $atts));
  274.  
  275.  
  276. $order = strtoupper($order);
  277.  
  278. $items_count = 0;
  279. $row_count = 0;
  280. $items_row = 0;
  281. $items_src = null;
  282.  
  283. $paged = get_query_var('paged') ? get_query_var('paged') : 1;
  284.  
  285. if ($id == null){
  286. $query = array( 'post_type' => 'project', 'orderby'=> 'ID', 'order' => $order, 'posts_per_page' => $items, 'paged' => $paged );
  287.  
  288. if ($service != null){
  289. $query['tax_query'] = array(
  290. array(
  291. 'taxonomy' => 'service',
  292. 'field' => 'slug',
  293. 'terms' => array( $service )
  294. ));
  295. }
  296.  
  297. $wp_query_project = new WP_Query($query);
  298. }else{
  299. $select_items = array();
  300.  
  301. if (is_numeric($id)){
  302. // if we have only one ID
  303. $select_items[] = $id ;
  304. }else{
  305. //If there are more ids separated by comma
  306. $tmp_id = explode(',', trim($id));
  307. foreach($tmp_id as $key => $item_id){
  308. if (is_numeric($item_id)){
  309. $select_items[] = $item_id ;
  310. }
  311. }
  312. }
  313.  
  314. $items = count($select_items);
  315. $wp_query_project = new WP_Query(array( 'post_type' => 'project', 'posts_per_page'=>$items, 'post__in' => $select_items, 'order'=> $order, 'orderby'=> 'id'));
  316. }
  317.  
  318. while ($wp_query_project->have_posts()) : $wp_query_project->the_post();
  319. global $post;
  320.  
  321. $items_count++;
  322. $items_row++;
  323.  
  324. $extra_class = null;
  325.  
  326. $image_id = get_post_thumbnail_id();
  327. $image_small_preview = wp_get_attachment_image_src($image_id,'project-preview-small');
  328. $image_large = wp_get_attachment_image_src($image_id,'large');
  329.  
  330. if ($items_row == 3){ $extra_class = "last"; $row_count ++; }
  331. if ($items_row == 1){
  332. if ($row_count > 0) { $items_src .= '<div class="hr"></div>'; }
  333. $items_src .= '<div class="row fixed">';
  334.  
  335. }
  336.  
  337. $items_src .='<div class="portfolio-item col-220 '.$extra_class.'">';
  338.  
  339. if ($image_id){
  340. $items_src .= '<p><a href="'.$image_large[0].'" class="portfolio-item-preview" rel="prettyPhoto[projects]" title="'.get_the_excerpt().'"><img src="'.$image_small_preview[0].'" width="220" height="300" alt="'.get_the_title().'" /></a></p>';
  341. }else{
  342. $items_src.= ewf_message(__('There is no featured image, please add one!', EWF_SETUP_THEME_DOMAIN));
  343. }
  344.  
  345. $items_src .='<h3><a href="'.get_permalink().'">'.get_the_title().'</a></h3>
  346. <p>'.get_the_excerpt().'</p>';
  347.  
  348. $items_src .= '<p><a href="'.get_permalink().'">'.__('Read More', EWF_SETUP_THEME_DOMAIN).'</a></p></div>';
  349.  
  350. if ($items_row == 3){
  351. $items_row = 0;
  352. $items_src .= '</div>';
  353. }else {
  354. if ($wp_query_project->post_count == $items_count){ $items_src .= '</div>'; }
  355. }
  356.  
  357. endwhile;
  358.  
  359. wp_reset_query();
  360.  
  361. if ($nav == "true"){
  362. $items_src .= '<div class="hr"></div>';
  363. $items_src .= '<div class="fixed">'.ewf_projects_pagination($items, $wp_query_project).'</div>';
  364. }
  365.  
  366.  
  367. if ($link){
  368. $items_src .= '<div class="fixed"><a href="'.$url.'">'.$title.'</a></div>';
  369. }
  370.  
  371. return $items_src;
  372. }
  373.  
  374. function ewf_register_type_project() {
  375. register_post_type('project',
  376.  
  377. array(
  378. 'labels' => array(
  379. 'name' => __( 'Projects' ,EWF_SETUP_THEME_DOMAIN ),
  380. 'singular_name' => __( 'Project' ,EWF_SETUP_THEME_DOMAIN ),
  381. 'add_new' => __( 'Add New' ,EWF_SETUP_THEME_DOMAIN ),
  382. 'add_new_item' => __( 'Add New Project' ,EWF_SETUP_THEME_DOMAIN ),
  383. 'edit' => __( 'Edit' ,EWF_SETUP_THEME_DOMAIN ),
  384. 'edit_item' => __( 'Edit Project' ,EWF_SETUP_THEME_DOMAIN ),
  385. 'new_item' => __( 'New Project' ,EWF_SETUP_THEME_DOMAIN ),
  386. 'view' => __( 'View Project' ,EWF_SETUP_THEME_DOMAIN ),
  387. 'view_item' => __( 'View Project' ,EWF_SETUP_THEME_DOMAIN ),
  388. 'search_items' => __( 'Search Projects' ,EWF_SETUP_THEME_DOMAIN ),
  389. 'not_found' => __( 'No projects found' ,EWF_SETUP_THEME_DOMAIN ),
  390. 'not_found_in_trash' => __( 'No projects found in Trash' ,EWF_SETUP_THEME_DOMAIN ),
  391. 'parent' => __( 'Parent project' ,EWF_SETUP_THEME_DOMAIN ),
  392. ),
  393. 'public' => true,
  394. 'rewrite' => true,
  395. 'slug' => 'project',
  396. 'show_ui' => true,
  397. 'supports' => array('title', 'editor', 'thumbnail', 'excerpt')
  398. ));
  399. }
  400.  
  401. function ewf_register_project_taxonomies() {
  402. register_taxonomy( 'service', 'project',
  403. array( 'hierarchical' => true,
  404. 'slug' => 'service',
  405. 'label' => __('Services', EWF_SETUP_THEME_DOMAIN),
  406. 'query_var' => true,
  407. 'rewrite' => true ));
  408. }
  409.  
  410. function ewf_projects_pagination($range = 4, $query){
  411. $src_nav = null;
  412.  
  413. $class_current = 'current';
  414. $current_page = $query->query_vars['paged'];
  415. $max_page = 0;
  416.  
  417. // How much pages do we have?
  418. if ( !$max_page ) {
  419. $max_page = $query->max_num_pages;
  420. }
  421.  
  422. $src_nav .= '<ul class="pagination fixed">';
  423.  
  424. // We need the pagination only if there are more than 1 page
  425. if($max_page > 1){
  426.  
  427. $src_nav .= '<li><strong>'.__('Page ', EWF_SETUP_THEME_DOMAIN).$current_page.' of '.$max_page.'</strong></li>';
  428.  
  429. if(!$current_page){
  430. $current_page = 1;
  431. }
  432. // On the first page, don't put the First page link
  433. //if($current_page != 1){
  434. // $src_nav.= "<li><a href=" . get_pagenum_link(1) . "> First </a></li>";
  435. //}
  436.  
  437. // We need the sliding effect only if there are more pages than is the sliding range
  438. if($max_page > $range){
  439. // When closer to the beginning
  440. if($current_page < $range){
  441. for($i = 1; $i <= ($range + 1); $i++){
  442.  
  443. $src_nav.= "<li ";
  444. if($i==$current_page) $src_nav.= "class='".$class_current."'";
  445.  
  446. $src_nav.= "><a href='" . get_pagenum_link($i) ."'";
  447. $src_nav.= ">$i</a></li>";
  448. }
  449. }
  450. // When closer to the end
  451. elseif($current_page >= ($max_page - ceil(($range/2)))){
  452. for($i = $max_page - $range; $i <= $max_page; $i++){
  453. $src_nav.= "<li ";
  454.  
  455. if($i==$current_page) $src_nav.= "class='".$class_current."'";
  456.  
  457. $src_nav.= "><a href='" . get_pagenum_link($i) ."'";
  458. $src_nav.= ">$i</a></li>";
  459. }
  460. }
  461. // Somewhere in the middle
  462. elseif($current_page >= $range && $current_page < ($max_page - ceil(($range/2)))){
  463. for($i = ($current_page - ceil($range/2)); $i <= ($current_page + ceil(($range/2))); $i++){
  464. $src_nav.= "<li ";
  465.  
  466. if($i==$current_page) $src_nav.= "class='".$class_current."'";
  467.  
  468. $src_nav.= "><a href='" . get_pagenum_link($i) ."'";
  469. $src_nav.= ">$i</a></li>";
  470. }
  471. }
  472. }
  473. // Less pages than the range, no sliding effect needed
  474. else{
  475. for($i = 1; $i <= $max_page; $i++){
  476. $src_nav.= "<li ";
  477.  
  478. if($i==$current_page) $src_nav.= "class='".$class_current."'";
  479.  
  480. $src_nav.= "><a href='" . get_pagenum_link($i) ."'";
  481. $src_nav.= ">$i</a></li>";
  482. }
  483. }
  484. }
  485.  
  486. $src_nav .= '</ul>';
  487.  
  488.  
  489. return $src_nav;
  490. }
  491.  
  492. ?>
  493.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement