ajayver

Impreza Portfolio shortcode with random option

Aug 22nd, 2014
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.21 KB | None | 0 0
  1. add_action( 'after_setup_theme', 'child_theme_setup', 20 );
  2.  
  3. function child_theme_setup() {
  4.     remove_shortcode('vc_portfolio');
  5.     add_shortcode('vc_portfolio', 'my_portfolio');
  6. }
  7.  
  8. function my_portfolio($attributes, $content) {
  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.             'with_indents' => false,
  18.             'random' => false
  19.         ), $attributes);
  20.  
  21.     if ( ! in_array($attributes['columns'], array(2,3,4,5)))
  22.     {
  23.         $attributes['columns'] = 3;
  24.     }
  25.  
  26.     if ( ! in_array($attributes['ratio'], array('3:2','4:3','1:1', '2:3', '3:4',)))
  27.     {
  28.         $attributes['ratio'] = '3:2';
  29.     }
  30.  
  31.     $attributes['ratio'] = str_replace(':', '-', $attributes['ratio']);
  32.  
  33.     global $wp_query;
  34.  
  35.     $attributes['items'] = intval($attributes['items']);
  36.     $portfolio_items = (is_integer($attributes['items']) AND $attributes['items'] > 0)?$attributes['items']:$attributes['columns'];
  37.  
  38.     global $paged;
  39.  
  40.     if (is_front_page()) {
  41.         $page_string = 'page';
  42.     } else {
  43.         $page_string = 'paged';
  44.     }
  45.  
  46.     if ($attributes['pagination'] == 1 OR $attributes['pagination'] == 'yes') {
  47.         $paged = get_query_var($page_string) ? get_query_var($page_string) : 1;
  48.     } else {
  49.         $paged = 1;
  50.     }
  51.  
  52.     $args = array(
  53.         'post_type'         => 'us_portfolio',
  54.         'posts_per_page'    => $portfolio_items,
  55.         'post_status'       => 'publish',
  56.         'orderby'           => 'date',
  57.         'order'             => 'DESC',
  58.         'paged'             => $paged
  59.     );
  60.  
  61.     if ($attributes['random'] == 1 OR $attributes['random'] == 'yes') {
  62.         $args['orderby'] = 'rand';
  63.     }
  64.  
  65.     $filters_html = $sortable_class = '';
  66.     $categories_slugs = null;
  67.  
  68.     if ( ! empty($attributes['category'])) {
  69.  
  70.         $categories_slugs = explode(',', $attributes['category']);
  71.         $args['tax_query'] = array(
  72.             array(
  73.                 'taxonomy' => 'us_portfolio_category',
  74.                 'field' => 'slug',
  75.                 'terms' => $categories_slugs
  76.             )
  77.         );
  78.     }
  79.  
  80.  
  81.     if ($attributes['filters'] == 1 OR $attributes['filters'] == 'yes') {
  82.         $categories = get_terms('us_portfolio_category');
  83.  
  84.         if ( ! empty($categories_slugs))
  85.         {
  86.             foreach ($categories as $cat_id => $category)
  87.             {
  88.                 if ( ! in_array($category->slug, $categories_slugs)) {
  89.                     unset($categories[$cat_id]);
  90.                 }
  91.             }
  92.         }
  93.  
  94.         if (count($categories) > 1) {
  95.             $filters_html .= '<div class="w-filters"><div class="w-filters-list">
  96.                                <div class="w-filters-item active">
  97.                                     <a class="w-filters-item-link" href="javascript:void(0);" data-filter="*">'.__('All', 'us').'</a>
  98.                                 </div>';
  99.             foreach($categories as $category) {
  100.                 $filters_html .= '<div class="w-filters-item">
  101.                                     <a class="w-filters-item-link" href="javascript:void(0);" data-filter=".'.$category->slug.'">'.$category->name.'</a>
  102.                                 </div>';
  103.             }
  104.  
  105.             $filters_html .= '</div></div>';
  106.         }
  107.     }
  108.  
  109.     if ($filters_html != '') {
  110.         $sortable_class = ' type_sortable';
  111.     }
  112.  
  113.     $with_indents_class = ($attributes['with_indents'] == 1 OR $attributes['with_indents'] == 'yes')?' with_indents':'';
  114.  
  115.     $output = '<div class="w-portfolio type_1 align_center columns_'.$attributes['columns'].' ratio_'.$attributes['ratio'].$sortable_class.$with_indents_class.'">
  116.                     '.$filters_html;
  117.  
  118.     $temp = $wp_query; $wp_query= null;
  119.  
  120.     $output .= '<div class="w-portfolio-list">';
  121.  
  122.     $wp_query = new WP_Query($args);
  123.  
  124.     $portfolio_order_counter = 0;
  125.  
  126.     while ( $wp_query->have_posts() )
  127.     {
  128.         $wp_query->the_post();
  129.         $portfolio_order_counter++;
  130.         $item_categories_links = '';
  131.         $item_categories_classes = '';
  132.         $item_categories = get_the_terms(get_the_ID(), 'us_portfolio_category');
  133.         if (is_array($item_categories))
  134.         {
  135.             foreach ($item_categories as $item_category)
  136.             {
  137.                 $item_categories_links .= $item_category->name.' / ';
  138.                 $item_categories_classes .= ' '.$item_category->slug;
  139.             }
  140.         }
  141.         if (function_exists('mb_strlen'))
  142.         {
  143.             if (mb_strlen($item_categories_links) > 0 )
  144.             {
  145.                 $item_categories_links = mb_substr($item_categories_links, 0, -2);
  146.             }
  147.         }
  148.         else
  149.         {
  150.             if (strlen($item_categories_links) > 0 )
  151.             {
  152.                 $item_categories_links = substr($item_categories_links, 0, -2);
  153.             }
  154.         }
  155.  
  156.         $link_ref = $link_target = '';
  157.         $link = esc_url( apply_filters( 'the_permalink', get_permalink() ) );
  158.  
  159.         if (rwmb_meta('us_custom_link') != ''){
  160.             $link = rwmb_meta('us_custom_link');
  161.             if (rwmb_meta('us_custom_link_blank') == 1){
  162.                 $link_target = ' target="_blank"';
  163.             }
  164.         }
  165.  
  166.         if (rwmb_meta('us_lightbox') == 1){
  167.             $img_id = get_post_thumbnail_id();
  168.             $link = wp_get_attachment_image_src($img_id, 'full');
  169.             $link = $link[0];
  170.             $link_ref = ' ref="magnificPopup"';
  171.         }
  172.  
  173.         $output .= '<div class="w-portfolio-item order_'.$portfolio_order_counter.$item_categories_classes.'">
  174.                         <div class="w-portfolio-item-h">
  175.                             <a class="w-portfolio-item-anchor"'.$link_target.$link_ref.' href="'.$link.'">
  176.                                 <div class="w-portfolio-item-image">';
  177.         if (has_post_thumbnail()) {
  178.             $output .= get_the_post_thumbnail(null, 'portfolio-list-'.$attributes['ratio'], array('class' => 'w-portfolio-item-image-first'));
  179.         } else {
  180.             $output .= '<img class="w-portfolio-item-image-first" src="'.get_template_directory_uri().'/img/placeholder/500x500.gif" alt="">';
  181.         }
  182.  
  183.         $additional_image = '';
  184.         if (rwmb_meta('us_additional_image') != '')
  185.         {
  186.             $additional_img_id = preg_replace('/[^\d]/', '', rwmb_meta('us_additional_image'));
  187.             $additional_img = wp_get_attachment_image_src($additional_img_id, 'portfolio-list-'.$attributes['ratio'], 0);
  188.  
  189.             if ( $additional_img != NULL )
  190.             {
  191.                 $additional_image = $additional_img[0];
  192.             }
  193.         }
  194.         if ($additional_image != '')
  195.         {
  196.             $output .= '<img class="w-portfolio-item-image-second" src="'.$additional_image.'" alt="">';
  197.         }
  198.                 $output .= '    </div>
  199.                                 <div class="w-portfolio-item-meta">
  200.                                     <div class="w-portfolio-item-meta-h">
  201.                                         <h2 class="w-portfolio-item-title">'.the_title('','', FALSE).'</h2>
  202.                                         <span class="w-portfolio-item-arrow"></span>
  203.                                     </div>
  204.                                 </div>
  205.                             </a>
  206.                         </div>
  207.                     </div>';
  208.     }
  209.  
  210.     $output .= '</div>';
  211.     if ($attributes['pagination'] == 1 OR $attributes['pagination'] == 'yes') {
  212.         if ($pagination = us_pagination()) {
  213.             $output .= '<div class="w-portfolio-pagination">
  214.                <div class="g-pagination align_center">
  215.                    '.$pagination.'
  216.                </div>
  217.            </div>';
  218.         }
  219.     }
  220.  
  221.     $output .= '</div>';
  222.  
  223.     wp_reset_postdata();
  224.     $wp_query= $temp;
  225.  
  226.     return $output;
  227.  
  228. }
  229.  
  230.  
  231. add_action('init', function()
  232. {
  233.     //Get VC gallery shortcode config
  234.     $shortcode_vc_portfolio_tmp = WPBMap::getShortCode('vc_portfolio');
  235.  
  236.     //print_r($shortcode_vc_portfolio_tmp);
  237.  
  238.     $shortcode_vc_portfolio_tmp['params'][] = array(
  239.             "type" => 'checkbox',
  240.             "heading" => __("Random order", "js_composer"),
  241.             "param_name" => "random",
  242.             "description" => __("If checked, the items will be shown in random order", "js_composer"),
  243.             "value" => Array(__("Turn on random order", "js_composer") => 'yes')
  244.         );
  245.  
  246.     //VC doesn't like even the thought of you changing the shortcode base, and errors out, so we unset it.
  247.     unset($shortcode_vc_portfolio_tmp['base']);
  248.  
  249.     //Update the actual parameter
  250.     vc_map_update('vc_portfolio', $shortcode_vc_portfolio_tmp);
  251. }, 100);
Advertisement
Add Comment
Please, Sign In to add comment