Advertisement
ajayver

Untitled

Aug 6th, 2014
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.45 KB | None | 0 0
  1.  
  2. add_action( 'after_setup_theme', 'child_theme_setup', 20 );
  3.  
  4. function child_theme_setup() {
  5.     remove_shortcode( 'blog' );
  6.     add_shortcode( 'blog', 'my_blog' );
  7. }
  8.  
  9. function my_blog($attributes, $content = null) {
  10.     $attributes = shortcode_atts(
  11.         array(
  12.             'posts' => 6,
  13.             'category' => null,
  14.             'ajax' => 0,
  15.         ), $attributes);
  16.  
  17.     if (is_numeric($attributes['posts']) AND $attributes['posts'] > 0)
  18.     {
  19.         $attributes['posts'] = ceil($attributes['posts']);
  20.     } else
  21.     {
  22.         $attributes['posts'] = 6;
  23.     }
  24.  
  25.     $args = array(
  26.         'post_status' => 'publish,future',
  27.         'post_type' => 'post',
  28.         'posts_per_page' => $attributes['posts'],
  29.         'post__not_in' => get_option('sticky_posts')
  30.     );
  31.  
  32.     if ( ! empty($attributes['category'])) {
  33.         $args['category_name'] = $attributes['category'];
  34.     }
  35.  
  36.     $posts = new WP_Query($args);
  37.     $max_num_pages = $posts->max_num_pages;
  38.  
  39.     $output =   '<div class="w-blog imgpos_atleft more_hidden">
  40.                    <div class="w-blog-h">
  41.                        <div class="w-blog-list">';
  42.  
  43.     while($posts->have_posts())
  44.     {
  45.         $posts->the_post();
  46.  
  47.         if (has_post_thumbnail()) {
  48.             $the_thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'blog-list');
  49.             $the_thumbnail = $the_thumbnail[0];
  50.         } else {
  51.             $the_thumbnail =  get_template_directory_uri() .'/img/placeholder/500x500.gif';
  52.         }
  53.  
  54.         $output .=          '<div class="w-blog-entry">
  55.                                <div class="w-blog-entry-h">
  56.                                    <a class="w-blog-entry-link" href="'.get_permalink(get_the_ID()).'">
  57.                                        <div class="w-blog-entry-preview">
  58.                                            <img src="'.$the_thumbnail.'" alt="">
  59.                                        </div>
  60.  
  61.                                        <h2 class="w-blog-entry-title">
  62.                                            <span class="w-blog-entry-title-h">'.get_the_title().'</span>
  63.                                        </h2>
  64.                                    </a>
  65.                                    <div class="w-blog-entry-body">
  66.                                        <div class="w-blog-entry-meta">
  67.                                            <div class="w-blog-entry-meta-date">
  68.                                                <span class="w-blog-entry-meta-date-month">'.get_the_date('M').'</span>
  69.                                                <span class="w-blog-entry-meta-date-day">'.get_the_date('d').'</span>
  70.                                                <span class="w-blog-entry-meta-date-year">'.get_the_date('Y').'</span>
  71.                                            </div>
  72.  
  73.                                            <div class="w-blog-entry-meta-comments">
  74.                                                <a class="w-blog-entry-meta-comments-h" href="'.get_permalink(get_the_ID()).'#comments"><i class="fa fa-comments"></i>'.get_comments_number().'</a>
  75.                                            </div>
  76.                                        </div>
  77.  
  78.                                        <div class="w-blog-entry-short">
  79.                                            '.apply_filters('the_excerpt', get_the_excerpt()).'
  80.                                        </div>
  81.  
  82.                                    </div>
  83.                                </div>
  84.                            </div>';
  85.     }
  86.  
  87.     $output .=          '</div>
  88.                    </div>
  89.                </div>';
  90.  
  91.     if ($max_num_pages > 1 AND $attributes['ajax'] == 1) {
  92.         $output .=
  93. '<script type="text/javascript">
  94. var page = 1,
  95. max_page = '.$max_num_pages.'
  96. jQuery(document).ready(function(){
  97. jQuery("#blog_load_more").click(function(){
  98.    jQuery(this).hide();
  99.    jQuery("#spinner").show();
  100.    jQuery.ajax({
  101.        type: "POST",
  102.        url: "'.admin_url('admin-ajax.php').'",
  103.        data: {
  104.            action: "blogPagination",
  105.            page: page+1,
  106.            per_page: '.$attributes['posts'].'
  107.        },
  108.        success: function(data, textStatus, XMLHttpRequest){
  109.            page++;
  110.            jQuery(".w-blog-list").append(data);
  111.            jQuery("#spinner").hide();
  112.            if (max_page > page) {
  113.                jQuery("#blog_load_more").show();
  114.            }
  115.            jQuery(window).resize();
  116.        },
  117.        error: function(MLHttpRequest, textStatus, errorThrown){
  118.            jQuery("#spinner").hide();
  119.            jQuery(this).show();
  120.        }
  121.    });
  122. });
  123. });
  124. </script>
  125. <div class="w-blog-load">
  126. <a href="javascript:void(0);" id="blog_load_more" class="g-btn type_default size_small"><span>'.__('Load More Posts', 'us').'</span></a>
  127. <img id="spinner" src="'.get_template_directory_uri().'/img/loader.gif" alt="loading..." style="display: none;">
  128. </div>';
  129.     }
  130.  
  131.     return $output;
  132. }
  133.  
  134. if ( ! function_exists('blogAjaxPagination'))
  135. {
  136.     function blogAjaxPagination() {
  137.         global $smof_data, $us_thumbnail_size;
  138.  
  139.         if (isset($_POST['page']) AND $_POST['page'] > 1)
  140.         {
  141.             $page = $_POST['page'];
  142.         }
  143.         else
  144.         {
  145.             return;
  146.         }
  147.  
  148.         $posts = new WP_Query();
  149.  
  150.         $lang_param = $per_page_param = '';
  151.  
  152.         if (defined('ICL_LANGUAGE_CODE'))
  153.         {
  154.             $lang_param = '&lang=' . ICL_LANGUAGE_CODE;
  155.         }
  156.  
  157.         if (isset($_POST['per_page'])) {
  158.             $per_page_param = '&posts_per_page='.$_POST['per_page'];
  159.         }
  160.  
  161.         $posts->query('paged='.$page.'&post_type=post&post_status=publish,future'.$per_page_param.$lang_param);
  162.  
  163.         $output = '';
  164.  
  165.         while($posts->have_posts())
  166.         {
  167.             $posts->the_post();
  168.  
  169.             if (has_post_thumbnail()) {
  170.                 $the_thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'blog-list');
  171.                 $the_thumbnail = $the_thumbnail[0];
  172.             } else {
  173.                 $the_thumbnail =  get_template_directory_uri() .'/img/placeholder/500x500.gif';
  174.             }
  175.  
  176.             $output .=          '<div class="w-blog-entry">
  177.                                    <div class="w-blog-entry-h">
  178.                                        <a class="w-blog-entry-link" href="'.get_permalink(get_the_ID()).'">
  179.                                            <div class="w-blog-entry-preview">
  180.                                                <img src="'.$the_thumbnail.'" alt="">
  181.                                            </div>
  182.  
  183.                                            <h2 class="w-blog-entry-title">
  184.                                                <span class="w-blog-entry-title-h">'.get_the_title().'</span>
  185.                                            </h2>
  186.                                        </a>
  187.                                        <div class="w-blog-entry-body">
  188.                                            <div class="w-blog-entry-meta">
  189.                                                <div class="w-blog-entry-meta-date">
  190.                                                    <span class="w-blog-entry-meta-date-month">'.get_the_date('M').'</span>
  191.                                                    <span class="w-blog-entry-meta-date-day">'.get_the_date('d').'</span>
  192.                                                    <span class="w-blog-entry-meta-date-year">'.get_the_date('Y').'</span>
  193.                                                </div>
  194.  
  195.                                                <div class="w-blog-entry-meta-comments">
  196.                                                    <a class="w-blog-entry-meta-comments-h" href="'.get_permalink(get_the_ID()).'#comments"><i class="fa fa-comments"></i>'.get_comments_number().'</a>
  197.                                                </div>
  198.                                            </div>
  199.  
  200.                                            <div class="w-blog-entry-short">
  201.                                                '.apply_filters('the_excerpt', get_the_excerpt()).'
  202.                                            </div>
  203.  
  204.                                        </div>
  205.                                    </div>
  206.                                </div>';
  207.         }
  208.  
  209.         echo $output;
  210.  
  211.         die();
  212.  
  213.     }
  214.  
  215.     add_action( 'wp_ajax_nopriv_blogPagination', 'blogAjaxPagination' );
  216.     add_action( 'wp_ajax_blogPagination', 'blogAjaxPagination' );
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement