function getAjaxData($category = '', $offset = '10') { $page = $_POST['page']; $postType = $_POST['postType']; $id = $_POST['id']; $offset = $_POST['offset']; $category = $_POST['category']; function validateIntegerInput($input) { $input = abs(intval($input)); filter_var($input, FILTER_SANITIZE_NUMBER_INT); if (!is_int($input) && !filter_var($input, FILTER_VALIDATE_INT)) { echo 'Invalid page input.'; exit(0); } } // Validate cross-site request forgery security token. if (!check_ajax_referer('ajax_fetch_nonce', 'token', false)) { header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden'); get_template_part('templates/index', '403'); exit(0); } if ($page === 'archive') { if ($postType === 'about' || $postType === 'work' || $postType === 'blog') { get_template_part('templates/index', $postType); } else { echo 'Invalid page input.'; } exit(0); // page, postId & postType; categoryId & offset } elseif ($page === 'single') { if ($postType === 'work') { $name = 'work_post'; } elseif ($postType === 'blog') { $name = 'blog_post'; } validateIntegerInput($id); global $post; $post = get_post($id); setup_postdata($post); get_template_part('templates/index', $name); wp_reset_postdata(); exit(0); } elseif ($page === 'category') { validateIntegerInput($id); global $post; $category = $id; $args = array('posts_per_page' => 10, 'offset' => '', 'category' => $category, 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' => '', 'post_type' => 'blog', 'post_mime_type' => '', 'post_parent' => '', 'post_status' => 'publish', 'suppress_filters' => true); $fetchedPosts = get_posts($args); ?> <div id="page_category_<?php echo $category; ?> " data-page-title="<?php echo strip_tags(esc_attr(get_the_category_by_id($category))); ?> "> <div class="title_wrapper"> <div class="titles"> <img src="<?php bloginfo('template_url'); ?> /img/title_blog@2x.png" alt=""> </div> </div> <?php foreach ($fetchedPosts as $post) { setup_postdata($post); ?> <article class="blog_list"> <h1 class="blog_title"><a href="<?php the_permalink(); ?> " data-link-type="postNavigation" data-page="single" data-post-type="blog" data-post-id="<?php the_ID(); ?> "><?php the_title(); ?> </a></h1> <h4 class="blog_date_categories_tags"><?php the_time('F j, Y'); ?> • <?php custom_the_category(', ', ''); the_tags(' • '); ?> </h4> </article><?php wp_reset_postdata(); } ?> </div> <?php exit(0); // Add if check here, so our final else can output a 404 error. } elseif (isset($offset) && isset($category)) { // Infinite scroll. global $post; validateIntegerInput($offset); validateIntegerInput($category); $args = array('posts_per_page' => 10, 'offset' => $offset, 'category' => $category, 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' => '', 'post_type' => 'blog', 'post_mime_type' => '', 'post_parent' => '', 'post_status' => 'publish', 'suppress_filters' => true); $fetchedPosts = get_posts($args); foreach ($fetchedPosts as $post) { setup_postdata($post); ?> <article class="blog_list"> <h1 class="blog_title"><a href="<?php the_permalink(); ?> " data-link-type="postNavigation" data-page="single" data-post-type="blog" data-post-id="<?php the_ID(); ?> "><?php the_title(); ?> </a></h1> <h4 class="blog_date_categories_tags"><?php the_time('F j, Y'); ?> • <?php custom_the_category(', ', ''); the_tags(' • '); ?> </h4> </article> <?php wp_reset_postdata(); } exit(0); } else { get_template_part('templates/index', '404'); exit(0); } }
$args = array('posts_per_page' => 15, 'offset' => 0, 'category' => '', 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' => '', 'post_type' => 'blog', 'post_mime_type' => '', 'post_parent' => '', 'post_status' => 'publish', 'suppress_filters' => true); $myposts = get_posts($args); foreach ($myposts as $post) { setup_postdata($post); ?> <article class="blog_list"> <h1 class="blog_title"><a href="<?php the_permalink(); ?> " data-link-type="postNavigation" data-page="single" data-post-type="blog" data-post-id="<?php the_ID(); ?> "><?php the_title(); ?> </a></h1> <h4 class="blog_date_categories_tags"><?php echo get_the_date(); ?> • <?php custom_the_category(', '); the_tags(' • '); ?> </h4> </article> <?php } wp_reset_postdata(); ?> </div>