Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'after_setup_theme', 'child_theme_setup', 20 );
- function child_theme_setup() {
- remove_shortcode('vc_portfolio');
- add_shortcode('vc_portfolio', 'my_portfolio');
- }
- function my_portfolio($attributes, $content) {
- $attributes = shortcode_atts(
- array(
- 'pagination' => false,
- 'filters' => false,
- 'columns' => 3,
- 'category' => null,
- 'items' => null,
- 'ratio' => '3:2',
- 'with_indents' => false,
- 'random' => false
- ), $attributes);
- if ( ! in_array($attributes['columns'], array(2,3,4,5)))
- {
- $attributes['columns'] = 3;
- }
- if ( ! in_array($attributes['ratio'], array('3:2','4:3','1:1', '2:3', '3:4',)))
- {
- $attributes['ratio'] = '3:2';
- }
- $attributes['ratio'] = str_replace(':', '-', $attributes['ratio']);
- global $wp_query;
- $attributes['items'] = intval($attributes['items']);
- $portfolio_items = (is_integer($attributes['items']) AND $attributes['items'] > 0)?$attributes['items']:$attributes['columns'];
- global $paged;
- if (is_front_page()) {
- $page_string = 'page';
- } else {
- $page_string = 'paged';
- }
- if ($attributes['pagination'] == 1 OR $attributes['pagination'] == 'yes') {
- $paged = get_query_var($page_string) ? get_query_var($page_string) : 1;
- } else {
- $paged = 1;
- }
- $args = array(
- 'post_type' => 'us_portfolio',
- 'posts_per_page' => $portfolio_items,
- 'post_status' => 'publish',
- 'orderby' => 'date',
- 'order' => 'DESC',
- 'paged' => $paged
- );
- if ($attributes['random'] == 1 OR $attributes['random'] == 'yes') {
- $args['orderby'] = 'rand';
- }
- $filters_html = $sortable_class = '';
- $categories_slugs = null;
- if ( ! empty($attributes['category'])) {
- $categories_slugs = explode(',', $attributes['category']);
- $args['tax_query'] = array(
- array(
- 'taxonomy' => 'us_portfolio_category',
- 'field' => 'slug',
- 'terms' => $categories_slugs
- )
- );
- }
- if ($attributes['filters'] == 1 OR $attributes['filters'] == 'yes') {
- $categories = get_terms('us_portfolio_category');
- if ( ! empty($categories_slugs))
- {
- foreach ($categories as $cat_id => $category)
- {
- if ( ! in_array($category->slug, $categories_slugs)) {
- unset($categories[$cat_id]);
- }
- }
- }
- if (count($categories) > 1) {
- $filters_html .= '<div class="w-filters"><div class="w-filters-list">
- <div class="w-filters-item active">
- <a class="w-filters-item-link" href="javascript:void(0);" data-filter="*">'.__('All', 'us').'</a>
- </div>';
- foreach($categories as $category) {
- $filters_html .= '<div class="w-filters-item">
- <a class="w-filters-item-link" href="javascript:void(0);" data-filter=".'.$category->slug.'">'.$category->name.'</a>
- </div>';
- }
- $filters_html .= '</div></div>';
- }
- }
- if ($filters_html != '') {
- $sortable_class = ' type_sortable';
- }
- $with_indents_class = ($attributes['with_indents'] == 1 OR $attributes['with_indents'] == 'yes')?' with_indents':'';
- $output = '<div class="w-portfolio type_1 align_center columns_'.$attributes['columns'].' ratio_'.$attributes['ratio'].$sortable_class.$with_indents_class.'">
- '.$filters_html;
- $temp = $wp_query; $wp_query= null;
- $output .= '<div class="w-portfolio-list">';
- $wp_query = new WP_Query($args);
- $portfolio_order_counter = 0;
- while ( $wp_query->have_posts() )
- {
- $wp_query->the_post();
- $portfolio_order_counter++;
- $item_categories_links = '';
- $item_categories_classes = '';
- $item_categories = get_the_terms(get_the_ID(), 'us_portfolio_category');
- if (is_array($item_categories))
- {
- foreach ($item_categories as $item_category)
- {
- $item_categories_links .= $item_category->name.' / ';
- $item_categories_classes .= ' '.$item_category->slug;
- }
- }
- if (function_exists('mb_strlen'))
- {
- if (mb_strlen($item_categories_links) > 0 )
- {
- $item_categories_links = mb_substr($item_categories_links, 0, -2);
- }
- }
- else
- {
- if (strlen($item_categories_links) > 0 )
- {
- $item_categories_links = substr($item_categories_links, 0, -2);
- }
- }
- $link_ref = $link_target = '';
- $link = esc_url( apply_filters( 'the_permalink', get_permalink() ) );
- if (rwmb_meta('us_custom_link') != ''){
- $link = rwmb_meta('us_custom_link');
- if (rwmb_meta('us_custom_link_blank') == 1){
- $link_target = ' target="_blank"';
- }
- }
- if (rwmb_meta('us_lightbox') == 1){
- $img_id = get_post_thumbnail_id();
- $link = wp_get_attachment_image_src($img_id, 'full');
- $link = $link[0];
- $link_ref = ' ref="magnificPopup"';
- }
- $output .= '<div class="w-portfolio-item order_'.$portfolio_order_counter.$item_categories_classes.'">
- <div class="w-portfolio-item-h">
- <a class="w-portfolio-item-anchor"'.$link_target.$link_ref.' href="'.$link.'">
- <div class="w-portfolio-item-image">';
- if (has_post_thumbnail()) {
- $output .= get_the_post_thumbnail(null, 'portfolio-list-'.$attributes['ratio'], array('class' => 'w-portfolio-item-image-first'));
- } else {
- $output .= '<img class="w-portfolio-item-image-first" src="'.get_template_directory_uri().'/img/placeholder/500x500.gif" alt="">';
- }
- $additional_image = '';
- if (rwmb_meta('us_additional_image') != '')
- {
- $additional_img_id = preg_replace('/[^\d]/', '', rwmb_meta('us_additional_image'));
- $additional_img = wp_get_attachment_image_src($additional_img_id, 'portfolio-list-'.$attributes['ratio'], 0);
- if ( $additional_img != NULL )
- {
- $additional_image = $additional_img[0];
- }
- }
- if ($additional_image != '')
- {
- $output .= '<img class="w-portfolio-item-image-second" src="'.$additional_image.'" alt="">';
- }
- $output .= ' </div>
- <div class="w-portfolio-item-meta">
- <div class="w-portfolio-item-meta-h">
- <h2 class="w-portfolio-item-title">'.the_title('','', FALSE).'</h2>
- <span class="w-portfolio-item-arrow"></span>
- </div>
- </div>
- </a>
- </div>
- </div>';
- }
- $output .= '</div>';
- if ($attributes['pagination'] == 1 OR $attributes['pagination'] == 'yes') {
- if ($pagination = us_pagination()) {
- $output .= '<div class="w-portfolio-pagination">
- <div class="g-pagination align_center">
- '.$pagination.'
- </div>
- </div>';
- }
- }
- $output .= '</div>';
- wp_reset_postdata();
- $wp_query= $temp;
- return $output;
- }
- add_action('init', function()
- {
- //Get VC gallery shortcode config
- $shortcode_vc_portfolio_tmp = WPBMap::getShortCode('vc_portfolio');
- //print_r($shortcode_vc_portfolio_tmp);
- $shortcode_vc_portfolio_tmp['params'][] = array(
- "type" => 'checkbox',
- "heading" => __("Random order", "js_composer"),
- "param_name" => "random",
- "description" => __("If checked, the items will be shown in random order", "js_composer"),
- "value" => Array(__("Turn on random order", "js_composer") => 'yes')
- );
- //VC doesn't like even the thought of you changing the shortcode base, and errors out, so we unset it.
- unset($shortcode_vc_portfolio_tmp['base']);
- //Update the actual parameter
- vc_map_update('vc_portfolio', $shortcode_vc_portfolio_tmp);
- }, 100);
Advertisement
Add Comment
Please, Sign In to add comment