ajayver

Untitled

Jun 16th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.39 KB | None | 0 0
  1. add_action( 'after_setup_theme', 'child_theme_setup', 20 );
  2.  
  3. function child_theme_setup() {
  4.     remove_shortcode( 'blog' );
  5.     add_shortcode( 'blog', 'my_blog' );
  6. }
  7.  
  8. function my_blog($attributes, $content = null)
  9.     {
  10.         $attributes = shortcode_atts(
  11.             array(
  12.                 'pagination' => false,
  13.                 'type' => 'square',
  14.                 'show_date' => null,
  15.                 'show_author' => null,
  16.                 'show_categories' => null,
  17.                 'show_tags' => null,
  18.                 'show_comments' => null,
  19.                 'show_read_more' => null,
  20.                 'category' => null,
  21.                 'items' => null,
  22.                 'columns' => null,
  23.             ), $attributes);
  24.  
  25.         $blog_thumbnails = array(
  26.             'square' => 'blog-list','rounded' => 'blog-list','masonry' => 'blog-grid'
  27.         );
  28.  
  29.         if ( ! in_array($attributes['type'], array('square','rounded','masonry')))
  30.         {
  31.             $attributes['type'] = 'square';
  32.         }
  33.  
  34.         if ( ! in_array($attributes['columns'], array(1,2,3)))
  35.         {
  36.             $attributes['columns'] = 1;
  37.         }
  38.  
  39.         if ($attributes['pagination'] == 1 OR $attributes['pagination'] == 'yes') {
  40.             $paged = get_query_var('paged') ? get_query_var('paged') : 1;
  41.         } else {
  42.             $paged = 1;
  43.         }
  44.  
  45.         $args = array(
  46. //            'post_type'       => 'us_portfolio',
  47.             'post_type'         => 'post',
  48.             'post_status'       => 'publish',
  49.             'orderby'           => 'date',
  50.             'order'             => 'DESC',
  51.             'paged'             => $paged
  52.         );
  53.  
  54.         $categories_slugs = null;
  55.  
  56.         if ( ! empty($attributes['category']))
  57.         {
  58.             $categories_slugs = explode(',', $attributes['category']);
  59.             $args['tax_query'] = array(
  60.                 array(
  61.                     'taxonomy' => 'category',
  62.                     'field' => 'slug',
  63.                     'terms' => $categories_slugs
  64.                 )
  65.             );
  66.         }
  67.  
  68.         $attributes['items'] = intval($attributes['items']);
  69.         if (is_integer($attributes['items']) AND $attributes['items'] > 0) {
  70.             $args['posts_per_page'] = $attributes['items'];
  71.         }
  72.  
  73.         $classes = 'w-blog columns_'.$attributes['columns'];;
  74.  
  75.         switch ($attributes['type']) {
  76.             case 'square': $classes .= ' imgpos_atleft';
  77.                 break;
  78.             case 'rounded': $classes .= ' imgpos_atleft circle';
  79.                 break;
  80.             case 'masonry': $classes .= ' imgpos_attop type_masonry';
  81.                 break;
  82.         }
  83.  
  84.         $output = '<div class="'.$classes.'">
  85.                        <div class="w-blog-list">';
  86.  
  87.  
  88.         global $wp_query;
  89.  
  90.         $temp = $wp_query; $wp_query= null;
  91.         $wp_query = new WP_Query(); $wp_query->query($args);
  92.  
  93.  
  94.         $us_thumbnail_size = $blog_thumbnails[$attributes['type']];
  95.         if (empty($us_thumbnail_size))
  96.         {
  97.             $us_thumbnail_size = 'blog-list';
  98.         }
  99.  
  100.         while ($wp_query->have_posts())
  101.         {
  102.             $wp_query->the_post();
  103.             global $smof_data;
  104.  
  105.             $post_format = get_post_format()?get_post_format():'standard';
  106.  
  107.             global $post;
  108.  
  109.             $preview = (has_post_thumbnail())?get_the_post_thumbnail(get_the_ID(), $us_thumbnail_size):'';
  110.  
  111.  
  112.             if (empty($preview) AND $us_thumbnail_size == 'blog-list')
  113.             {
  114.                 $preview = '<img src="'.get_template_directory_uri().'/img/placeholder/500x500.gif" alt="">';
  115.             }
  116.             $output .= '<div class="' . join( ' ', get_post_class( 'w-blog-entry', null ) ) . '">
  117.                 <div class="w-blog-entry-h">
  118.                    <a class="w-blog-entry-link" href="'.get_permalink().'">';
  119.             if ($preview) {
  120.                 $output .= '<span class="w-blog-entry-preview">'.$preview.'</span>';
  121.             }
  122. //            if ($post_format == 'quote')
  123. //            {
  124. //                $output .= '<div class="w-blog-entry-title">
  125. //                <blockquote class="w-blog-entry-title-h">'.get_the_title().'</blockquote>
  126. //                </div>';
  127. //            }
  128. //            else
  129. //            {
  130.                 $output .= '<h2 class="w-blog-entry-title"><span>'.get_the_title().'</span></h2>';
  131. //            }
  132.             $output .= '</a>
  133.                    <div class="w-blog-entry-body">
  134.                        <div class="w-blog-meta">';
  135.             if ($attributes['show_date'] == 1 OR $attributes['show_date'] == 'yes') {
  136.                 $output .= '<div class="w-blog-meta-date">
  137.                                <i class="fa fa-clock-o"></i>
  138.                                <span>'.get_the_date().'</span>
  139.                            </div>';
  140.             }
  141.             if ($attributes['show_author'] == 1 OR $attributes['show_author'] == 'yes') {
  142.                 $output .= '<div class="w-blog-meta-author">
  143.                                <i class="fa fa-user"></i>';
  144.                 if (get_the_author_meta('url')) {
  145.                     $output .= '<a href="'.esc_url( get_the_author_meta('url') ).'">'.get_the_author().'</a>';
  146.                 } else {
  147.                     $output .= '<span>'.get_the_author().'</span>';
  148.                 }
  149.                 $output .= '</div>';
  150.             }
  151.             if ($attributes['show_categories'] == 1 OR $attributes['show_categories'] == 'yes') {
  152.                 $output .= '<div class="w-blog-meta-category">
  153.                                <i class="fa fa-folder-open"></i>';
  154.                 $categories = get_the_category();
  155.                 $categories_output = '';
  156.                 $separator = ', ';
  157.                 if($categories){
  158.                     foreach($categories as $category) {
  159.                         $categories_output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
  160.                     }
  161.                 }
  162.                 $output .= trim($categories_output, $separator).'
  163.                                </div>';
  164.             }
  165.             if ($attributes['show_tags'] == 1 OR $attributes['show_tags'] == 'yes') {
  166.                 $tags = wp_get_post_tags($post->ID);
  167.                 if ($tags) {
  168.                     $output .= '<div class="w-blog-meta-tags">
  169.                                    <i class="fa fa-tags"></i>';
  170.  
  171.                     $tags_output = '';
  172.                     $separator = ', ';
  173.                     foreach($tags as $tag) {
  174.                         $tags_output .= '<a href="'.get_tag_link($tag->term_id).'">'.$tag->name.'</a>'.$separator;
  175.                     }
  176.  
  177.                     $output .= trim($tags_output, $separator).'
  178.                                    </div>';
  179.                 }
  180.             }
  181.             if ($attributes['show_comments'] == 1 OR $attributes['show_comments'] == 'yes') {
  182.  
  183.                 if ( ! (get_comments_number() == 0 AND ! comments_open() AND ! pings_open())) {
  184.                     $output .= '<div class="w-blog-meta-comments">';
  185.                     $output .= '<i class="fa fa-comments"></i>';
  186.                     $number = get_comments_number();
  187.  
  188.                     if ( 0 == $number ) {
  189.                         $comments_link = get_permalink() . '#respond';
  190.                     }
  191.                     else {
  192.                         $comments_link = esc_url(get_comments_link());
  193.                     }
  194.                     $output .= '<a href="'.$comments_link.'">';
  195.  
  196.                     if ( $number > 1 )
  197.                         $output .= str_replace('%', number_format_i18n($number), __('% Comments', 'us'));
  198.                     elseif ( $number == 0 )
  199.                         $output .= __('No Comments', 'us');
  200.                     else // must be one
  201.                         $output .= __('1 Comment', 'us');
  202.                     $output .= '</a></div>';
  203.                 }
  204.  
  205.             }
  206.             $output .= '</div>';
  207.  
  208.             $output .= '<div class="w-blog-entry-short">';
  209.  
  210.             $excerpt = get_the_content(get_the_ID());
  211.             $excerpt = do_shortcode($excerpt);
  212.  
  213.  
  214.             $excerpt = apply_filters('the_excerpt', $excerpt);
  215.             $excerpt = str_replace(']]>', ']]&gt;', $excerpt);
  216.             $excerpt_length = apply_filters('excerpt_length', 55);
  217.             $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
  218.             $excerpt = wp_trim_words( $excerpt, $excerpt_length, $excerpt_more );
  219.  
  220.             $output .= $excerpt;
  221.  
  222.             $output .= '</div>';
  223.  
  224.             if ($attributes['show_read_more'] == 1 OR $attributes['show_read_more'] == 'yes') {
  225.                 $output .= '<a class="w-blog-entry-more g-btn color_faded outlined size_small" href="'.get_permalink().'"><span>'.__('Read More', 'us').'</span></a>';
  226.             }
  227.  
  228.             $output .= '</div>
  229.                 </div>
  230.            </div>';
  231.         }
  232.  
  233.         $output .= '</div></div>';
  234.  
  235.         if ($attributes['pagination'] == 1 OR $attributes['pagination'] == 'yes') {
  236.             if ($pagination = us_pagination()) {
  237.                 $output .= '<div class="w-portfolio-pagination">
  238.                    <div class="g-pagination align_center">
  239.                        '.$pagination.'
  240.                    </div>
  241.                </div>';
  242.             }
  243.         }
  244.  
  245.         wp_reset_postdata();
  246.         $wp_query= $temp;
  247.  
  248.         return $output;
  249.     }
Advertisement
Add Comment
Please, Sign In to add comment