ajayver

Untitled

Jul 28th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.78 KB | None | 0 0
  1. add_action( 'after_setup_theme', 'child_theme_setup', 20 );
  2.  
  3. function child_theme_setup() {
  4.     add_shortcode( 'video_portfolio', 'video_portfolio' );
  5. }
  6.  
  7. function video_portfolio($attributes, $content)
  8.     {
  9.         $attributes = shortcode_atts(
  10.             array(
  11.                 'pagination' => false,
  12.                 'filters' => false,
  13.                 'columns' => 3,
  14.                 'category' => null,
  15.                 'items' => null,
  16.                 'ratio' => '3:2',
  17.                 'no_indents' => false,
  18.             ), $attributes);
  19.  
  20.         if ( ! in_array($attributes['columns'], array(2,3,4,5)))
  21.         {
  22.             $attributes['columns'] = 3;
  23.         }
  24.  
  25.         if ( ! in_array($attributes['ratio'], array('3:2','4:3','1:1', '2:3', '3:4',)))
  26.         {
  27.             $attributes['ratio'] = '3:2';
  28.         }
  29.  
  30.         $attributes['ratio'] = str_replace(':', '-', $attributes['ratio']);
  31.  
  32.         global $wp_query;
  33.  
  34.         $attributes['items'] = intval($attributes['items']);
  35.         $portfolio_items = (is_integer($attributes['items']) AND $attributes['items'] > 0)?$attributes['items']:$attributes['columns'];
  36.         if ($attributes['pagination'] == 1 OR $attributes['pagination'] == 'yes') {
  37.             $paged = get_query_var('paged') ? get_query_var('paged') : 1;
  38.         } else {
  39.             $paged = 1;
  40.         }
  41.         $args = array(
  42.             'post_type'         => 'us_portfolio',
  43.             'posts_per_page'    => $portfolio_items,
  44.             'post_status'       => 'publish',
  45.             'orderby'           => 'date',
  46.             'order'             => 'DESC',
  47.             'paged'             => $paged
  48.         );
  49.  
  50.         $filters_html = $sortable_class = '';
  51.         $categories_slugs = null;
  52.  
  53.         if ( ! empty($attributes['category'])) {
  54.  
  55.             $categories_slugs = explode(',', $attributes['category']);
  56.             $args['tax_query'] = array(
  57.                 array(
  58.                     'taxonomy' => 'us_portfolio_category',
  59.                     'field' => 'slug',
  60.                     'terms' => $categories_slugs
  61.                 )
  62.             );
  63.         }
  64.  
  65.  
  66.         if ($attributes['filters'] == 1 OR $attributes['filters'] == 'yes') {
  67.             $categories = get_terms('us_portfolio_category');
  68.  
  69.             if ( ! empty($categories_slugs))
  70.             {
  71.                 foreach ($categories as $cat_id => $category)
  72.                 {
  73.                     if ( ! in_array($category->slug, $categories_slugs)) {
  74.                         unset($categories[$cat_id]);
  75.                     }
  76.                 }
  77.             }
  78.  
  79.             if (count($categories) > 1) {
  80.                 $filters_html .= '<div class="w-filters">
  81.                            <div class="w-filters-h">
  82.                                <div class="w-filters-list">
  83.                                    <div class="w-filters-item active">
  84.                                        <a class="w-filters-item-link" href="javascript:void(0);" data-filter="*">'.__('All', 'us').'</a>
  85.                                    </div>';
  86.                 foreach($categories as $category) {
  87.                     $filters_html .= '<div class="w-filters-item">
  88.                                    <a class="w-filters-item-link" href="javascript:void(0);" data-filter=".'.$category->slug.'">'.$category->name.'</a>
  89.                                </div>';
  90.                 }
  91.  
  92.                 $filters_html .= '</div>
  93.                            </div>
  94.                        </div>';
  95.             }
  96.         }
  97.  
  98.         if ($filters_html != '') {
  99.             $sortable_class = ' type_sortable';
  100.         }
  101.  
  102.         $no_indents_class = ($attributes['no_indents'] == 1 OR $attributes['no_indents'] == 'yes')?' indents_none':'';
  103.  
  104.         $output = '<div class="w-portfolio columns_'.$attributes['columns'].' ratio_'.$attributes['ratio'].$sortable_class.$no_indents_class.'">
  105.             <div class="w-portfolio-h">'.$filters_html;
  106.  
  107.         $temp = $wp_query; $wp_query= null;
  108.  
  109.         $output .= '<div class="w-portfolio-list">
  110.                     <div class="w-portfolio-list-h">';
  111.  
  112.         $wp_query = new WP_Query($args);
  113.  
  114.         $portfolio_order_counter = 0;
  115.  
  116.         while ( $wp_query->have_posts() )
  117.         {
  118.             $wp_query->the_post();
  119.             $portfolio_order_counter++;
  120.             $item_categories_links = '';
  121.             $item_categories_classes = '';
  122.             $item_categories = get_the_terms(get_the_ID(), 'us_portfolio_category');
  123.             if (is_array($item_categories))
  124.             {
  125.                 foreach ($item_categories as $item_category)
  126.                 {
  127.                     $item_categories_links .= $item_category->name.' / ';
  128.                     $item_categories_classes .= ' '.$item_category->slug;
  129.                 }
  130.             }
  131.             if (mb_strlen($item_categories_links) > 0 )
  132.             {
  133.                 $item_categories_links = mb_substr($item_categories_links, 0, -2);
  134.             }
  135.  
  136.             $link_ref = $link_target = '';
  137.             $link = esc_url( apply_filters( 'the_permalink', get_permalink() ) );
  138.  
  139.             if (rwmb_meta('us_custom_link') != ''){
  140.                 $link = rwmb_meta('us_custom_link');
  141.                 if (rwmb_meta('us_custom_link_blank') == 1){
  142.                     $link_target = ' target="_blank"';
  143.                 }
  144.             }
  145.  
  146.             if (rwmb_meta('us_lightbox') == 1){
  147.                 $img_id = get_post_thumbnail_id();
  148.                 $link = wp_get_attachment_image_src($img_id, 'full');
  149.                 $link = $link[0];
  150.                 $link_ref = ' ref="magnificPopup"';
  151.             }
  152.  
  153.             $output .= '<div class="w-portfolio-item order_'.$portfolio_order_counter.$item_categories_classes.'">
  154.                             <div class="w-portfolio-item-h">
  155.                                 <a class="w-portfolio-item-anchor"'.$link_target.$link_ref.' rel="wp-video-lightbox" href="'.$link.'">
  156.                                     <div class="w-portfolio-item-image">';
  157.             if (has_post_thumbnail()) {
  158.                 $output .= get_the_post_thumbnail(null, 'portfolio-list-'.$attributes['ratio'], array('class' => 'w-portfolio-item-image-first'));
  159.             } else {
  160.                 $output .= '<img class="w-portfolio-item-image-first" src="'.get_template_directory_uri().'/img/placeholder/500x500.gif" alt="">';
  161.             }
  162.  
  163.             $additional_image = '';
  164.             if (rwmb_meta('us_additional_image') != '')
  165.             {
  166.                 $additional_img_id = preg_replace('/[^\d]/', '', rwmb_meta('us_additional_image'));
  167.                 $additional_img = wpb_getImageBySize(array('attach_id' => $additional_img_id, 'thumb_size' => 'portfolio-list-'.$attributes['ratio'] ));
  168.  
  169.                 if ( $additional_img != NULL )
  170.                 {
  171.                     $additional_img = wp_get_attachment_image_src($additional_img_id, 'portfolio-list-'.$attributes['ratio']);
  172.                     $additional_image = $additional_img[0];
  173.                 }
  174.             }
  175.             if ($additional_image != '')
  176.             {
  177.                 $output .= '<img class="w-portfolio-item-image-second" src="'.$additional_image.'" alt="">';
  178.             }
  179.                     $output .= '                <div class="w-portfolio-item-meta">
  180.                                                 <h2 class="w-portfolio-item-title">'.the_title('','', FALSE).'</h2>
  181.                                                 <span class="w-portfolio-item-arrow"></span>
  182.                                             </div>
  183.                                     </div>
  184.                                 </a>
  185.                             </div>
  186.                         </div>';
  187.         }
  188.  
  189.         $output .= '</div>
  190.                 </div>';
  191.         if ($attributes['pagination'] == 1 OR $attributes['pagination'] == 'yes') {
  192.             if ($pagination = us_pagination()) {
  193.                 $output .= '<div class="w-portfolio-pagination">
  194.                    <div class="g-pagination align_center">
  195.                        '.$pagination.'
  196.                    </div>
  197.                </div>';
  198.             }
  199.         }
  200.  
  201.         $output .= '</div>
  202.            </div>';
  203.  
  204.         wp_reset_postdata();
  205.         $wp_query= $temp;
  206.  
  207.         return $output;
  208.  
  209.     }
Advertisement
Add Comment
Please, Sign In to add comment