View difference between Paste ID: MVdYvpG3 and j5k3r3rF
SHOW: | | - or go back to the newest paste.
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
    $attributes = shortcode_atts(
10
        array(
11
            'posts' => 6,
12
            'category' => null,
13
            'ajax' => 0,
14
            'reverse' => 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 ( $attributes['reverse'] == 1) {
33
        $args['order'] = 'ASC';
34
    }
35
36
    if ( ! empty($attributes['category'])) {
37
        $args['category_name'] = $attributes['category'];
38
    }
39
40
    $posts = new WP_Query($args);
41
    $max_num_pages = $posts->max_num_pages;
42
43
    $output =   '<div class="w-blog imgpos_atleft more_hidden">
44
                    <div class="w-blog-h">
45
                        <div class="w-blog-list">';
46
47
    while($posts->have_posts())
48
    {
49
        $posts->the_post();
50
51
        if (has_post_thumbnail()) {
52
            $the_thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'blog-list');
53
            $the_thumbnail = $the_thumbnail[0];
54
        } else {
55
            $the_thumbnail =  get_template_directory_uri() .'/img/placeholder/500x500.gif';
56
        }
57
58
        $output .=          '<div class="w-blog-entry">
59
                                <div class="w-blog-entry-h">
60
                                    <a class="w-blog-entry-link" href="'.get_permalink(get_the_ID()).'">
61
                                        <div class="w-blog-entry-preview">
62
                                            <img src="'.$the_thumbnail.'" alt="">
63
                                        </div>
64
65
                                        <h2 class="w-blog-entry-title">
66
                                            <span class="w-blog-entry-title-h">'.get_the_title().'</span>
67
                                        </h2>
68
                                    </a>
69
                                    <div class="w-blog-entry-body">
70
                                        <div class="w-blog-entry-meta">
71
                                            <div class="w-blog-entry-meta-date">
72
                                                <span class="w-blog-entry-meta-date-month">'.get_the_date('M').'</span>
73
                                                <span class="w-blog-entry-meta-date-day">'.get_the_date('d').'</span>
74
                                                <span class="w-blog-entry-meta-date-year">'.get_the_date('Y').'</span>
75
                                            </div>
76
77
                                            <div class="w-blog-entry-meta-comments">
78
                                                <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>
79
                                            </div>
80
                                        </div>
81
82
                                        <div class="w-blog-entry-short">
83
                                            '.apply_filters('the_excerpt', get_the_excerpt()).'
84
                                        </div>
85
86
                                    </div>
87
                                </div>
88
                            </div>';
89
    }
90
91
    $output .=          '</div>
92
                    </div>
93
                </div>';
94
95
    if ($max_num_pages > 1 AND $attributes['ajax'] == 1) {
96
        $output .=
97
'<script type="text/javascript">
98
var page = 1,
99
max_page = '.$max_num_pages.'
100
jQuery(document).ready(function(){
101
jQuery("#blog_load_more").click(function(){
102
    jQuery(this).hide();
103
    jQuery("#spinner").show();
104
    jQuery.ajax({
105
        type: "POST",
106
        url: "'.admin_url('admin-ajax.php').'",
107
        data: {
108
            action: "blogPagination",
109
            page: page+1,
110
            per_page: '.$attributes['posts'].'
111
        },
112
        success: function(data, textStatus, XMLHttpRequest){
113
            page++;
114
            jQuery(".w-blog-list").append(data);
115
            jQuery("#spinner").hide();
116
            if (max_page > page) {
117
                jQuery("#blog_load_more").show();
118
            }
119
            jQuery(window).resize();
120
        },
121
        error: function(MLHttpRequest, textStatus, errorThrown){
122
            jQuery("#spinner").hide();
123
            jQuery(this).show();
124
        }
125
    });
126
});
127
});
128
</script>
129
<div class="w-blog-load">
130
<a href="javascript:void(0);" id="blog_load_more" class="g-btn type_default size_small"><span>'.__('Load More Posts', 'us').'</span></a>
131
<img id="spinner" src="'.get_template_directory_uri().'/img/loader.gif" alt="loading..." style="display: none;">
132
</div>';
133
    }
134
135
    return $output;
136
}
137
138
if ( ! function_exists('blogAjaxPagination'))
139
{
140
    function blogAjaxPagination() {
141
        global $smof_data, $us_thumbnail_size;
142
143
        if (isset($_POST['page']) AND $_POST['page'] > 1)
144
        {
145
            $page = $_POST['page'];
146
        }
147
        else
148
        {
149
            return;
150
        }
151
152
        $posts = new WP_Query();
153
154
        $lang_param = $per_page_param = '';
155
156
        if (defined('ICL_LANGUAGE_CODE'))
157
        {
158
            $lang_param = '&lang=' . ICL_LANGUAGE_CODE;
159
        }
160
161
        if (isset($_POST['per_page'])) {
162
            $per_page_param = '&posts_per_page='.$_POST['per_page'];
163
        }
164
165
        $posts->query('paged='.$page.'&post_type=post&post_status=publish,future'.$per_page_param.$lang_param);
166
167
        $output = '';
168
169
        while($posts->have_posts())
170
        {
171
            $posts->the_post();
172
173
            if (has_post_thumbnail()) {
174
                $the_thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'blog-list');
175
                $the_thumbnail = $the_thumbnail[0];
176
            } else {
177
                $the_thumbnail =  get_template_directory_uri() .'/img/placeholder/500x500.gif';
178
            }
179
180
            $output .=          '<div class="w-blog-entry">
181
                                    <div class="w-blog-entry-h">
182
                                        <a class="w-blog-entry-link" href="'.get_permalink(get_the_ID()).'">
183
                                            <div class="w-blog-entry-preview">
184
                                                <img src="'.$the_thumbnail.'" alt="">
185
                                            </div>
186
187
                                            <h2 class="w-blog-entry-title">
188
                                                <span class="w-blog-entry-title-h">'.get_the_title().'</span>
189
                                            </h2>
190
                                        </a>
191
                                        <div class="w-blog-entry-body">
192
                                            <div class="w-blog-entry-meta">
193
                                                <div class="w-blog-entry-meta-date">
194
                                                    <span class="w-blog-entry-meta-date-month">'.get_the_date('M').'</span>
195
                                                    <span class="w-blog-entry-meta-date-day">'.get_the_date('d').'</span>
196
                                                    <span class="w-blog-entry-meta-date-year">'.get_the_date('Y').'</span>
197
                                                </div>
198
199
                                                <div class="w-blog-entry-meta-comments">
200
                                                    <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>
201
                                                </div>
202
                                            </div>
203
204
                                            <div class="w-blog-entry-short">
205
                                                '.apply_filters('the_excerpt', get_the_excerpt()).'
206
                                            </div>
207
208
                                        </div>
209
                                    </div>
210
                                </div>';
211
        }
212
213
        echo $output;
214
215
        die();
216
217
    }
218
219
    add_action( 'wp_ajax_nopriv_blogPagination', 'blogAjaxPagination' );
220
    add_action( 'wp_ajax_blogPagination', 'blogAjaxPagination' );
221
}