Advertisement
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( 'blog' );
- add_shortcode( 'blog', 'my_blog' );
- }
- function my_blog($attributes, $content = null) {
- $attributes = shortcode_atts(
- array(
- 'posts' => 6,
- 'category' => null,
- 'ajax' => 0,
- ), $attributes);
- if (is_numeric($attributes['posts']) AND $attributes['posts'] > 0)
- {
- $attributes['posts'] = ceil($attributes['posts']);
- } else
- {
- $attributes['posts'] = 6;
- }
- $args = array(
- 'post_status' => 'publish,future',
- 'post_type' => 'post',
- 'posts_per_page' => $attributes['posts'],
- 'post__not_in' => get_option('sticky_posts')
- );
- if ( ! empty($attributes['category'])) {
- $args['category_name'] = $attributes['category'];
- }
- $posts = new WP_Query($args);
- $max_num_pages = $posts->max_num_pages;
- $output = '<div class="w-blog imgpos_atleft more_hidden">
- <div class="w-blog-h">
- <div class="w-blog-list">';
- while($posts->have_posts())
- {
- $posts->the_post();
- if (has_post_thumbnail()) {
- $the_thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'blog-list');
- $the_thumbnail = $the_thumbnail[0];
- } else {
- $the_thumbnail = get_template_directory_uri() .'/img/placeholder/500x500.gif';
- }
- $output .= '<div class="w-blog-entry">
- <div class="w-blog-entry-h">
- <a class="w-blog-entry-link" href="'.get_permalink(get_the_ID()).'">
- <div class="w-blog-entry-preview">
- <img src="'.$the_thumbnail.'" alt="">
- </div>
- <h2 class="w-blog-entry-title">
- <span class="w-blog-entry-title-h">'.get_the_title().'</span>
- </h2>
- </a>
- <div class="w-blog-entry-body">
- <div class="w-blog-entry-meta">
- <div class="w-blog-entry-meta-date">
- <span class="w-blog-entry-meta-date-month">'.get_the_date('M').'</span>
- <span class="w-blog-entry-meta-date-day">'.get_the_date('d').'</span>
- <span class="w-blog-entry-meta-date-year">'.get_the_date('Y').'</span>
- </div>
- <div class="w-blog-entry-meta-comments">
- <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>
- </div>
- </div>
- <div class="w-blog-entry-short">
- '.apply_filters('the_excerpt', get_the_excerpt()).'
- </div>
- </div>
- </div>
- </div>';
- }
- $output .= '</div>
- </div>
- </div>';
- if ($max_num_pages > 1 AND $attributes['ajax'] == 1) {
- $output .=
- '<script type="text/javascript">
- var page = 1,
- max_page = '.$max_num_pages.'
- jQuery(document).ready(function(){
- jQuery("#blog_load_more").click(function(){
- jQuery(this).hide();
- jQuery("#spinner").show();
- jQuery.ajax({
- type: "POST",
- url: "'.admin_url('admin-ajax.php').'",
- data: {
- action: "blogPagination",
- page: page+1,
- per_page: '.$attributes['posts'].'
- },
- success: function(data, textStatus, XMLHttpRequest){
- page++;
- jQuery(".w-blog-list").append(data);
- jQuery("#spinner").hide();
- if (max_page > page) {
- jQuery("#blog_load_more").show();
- }
- jQuery(window).resize();
- },
- error: function(MLHttpRequest, textStatus, errorThrown){
- jQuery("#spinner").hide();
- jQuery(this).show();
- }
- });
- });
- });
- </script>
- <div class="w-blog-load">
- <a href="javascript:void(0);" id="blog_load_more" class="g-btn type_default size_small"><span>'.__('Load More Posts', 'us').'</span></a>
- <img id="spinner" src="'.get_template_directory_uri().'/img/loader.gif" alt="loading..." style="display: none;">
- </div>';
- }
- return $output;
- }
- if ( ! function_exists('blogAjaxPagination'))
- {
- function blogAjaxPagination() {
- global $smof_data, $us_thumbnail_size;
- if (isset($_POST['page']) AND $_POST['page'] > 1)
- {
- $page = $_POST['page'];
- }
- else
- {
- return;
- }
- $posts = new WP_Query();
- $lang_param = $per_page_param = '';
- if (defined('ICL_LANGUAGE_CODE'))
- {
- $lang_param = '&lang=' . ICL_LANGUAGE_CODE;
- }
- if (isset($_POST['per_page'])) {
- $per_page_param = '&posts_per_page='.$_POST['per_page'];
- }
- $posts->query('paged='.$page.'&post_type=post&post_status=publish,future'.$per_page_param.$lang_param);
- $output = '';
- while($posts->have_posts())
- {
- $posts->the_post();
- if (has_post_thumbnail()) {
- $the_thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'blog-list');
- $the_thumbnail = $the_thumbnail[0];
- } else {
- $the_thumbnail = get_template_directory_uri() .'/img/placeholder/500x500.gif';
- }
- $output .= '<div class="w-blog-entry">
- <div class="w-blog-entry-h">
- <a class="w-blog-entry-link" href="'.get_permalink(get_the_ID()).'">
- <div class="w-blog-entry-preview">
- <img src="'.$the_thumbnail.'" alt="">
- </div>
- <h2 class="w-blog-entry-title">
- <span class="w-blog-entry-title-h">'.get_the_title().'</span>
- </h2>
- </a>
- <div class="w-blog-entry-body">
- <div class="w-blog-entry-meta">
- <div class="w-blog-entry-meta-date">
- <span class="w-blog-entry-meta-date-month">'.get_the_date('M').'</span>
- <span class="w-blog-entry-meta-date-day">'.get_the_date('d').'</span>
- <span class="w-blog-entry-meta-date-year">'.get_the_date('Y').'</span>
- </div>
- <div class="w-blog-entry-meta-comments">
- <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>
- </div>
- </div>
- <div class="w-blog-entry-short">
- '.apply_filters('the_excerpt', get_the_excerpt()).'
- </div>
- </div>
- </div>
- </div>';
- }
- echo $output;
- die();
- }
- add_action( 'wp_ajax_nopriv_blogPagination', 'blogAjaxPagination' );
- add_action( 'wp_ajax_blogPagination', 'blogAjaxPagination' );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement