← Back
Editing: functions.php
<?php // ============================================================================= // FUNCTIONS.PHP // ----------------------------------------------------------------------------- // Overwrite or add your own custom functions to Pro in this file. // ============================================================================= // ============================================================================= // TABLE OF CONTENTS // ----------------------------------------------------------------------------- // 01. Enqueue Parent Stylesheet // 02. Additional Functions // ============================================================================= // Enqueue Parent Stylesheet // ============================================================================= add_filter( 'x_enqueue_parent_stylesheet', '__return_true' ); // Additional Functions // ============================================================================= /** * * Post Listing Shortcode * */ function theme_blog_list($atts, $content=null){ extract(shortcode_atts( array( "type" => "post", "template" => "blog", "count" => 6, "author" => '', "tax" => "", "term" =>"" ),$atts )); ob_start(); $args = array( 'post_type' => $type, 'posts_per_page' => $count, 'paged' => 1, 'post_status' => 'publish', 'post__not_in' => get_option( 'sticky_posts' ) ); if($author){ $args['author'] = $author; } if($tax && $term){ $args["tax_query"] = array( array( 'taxonomy' => $tax, 'field' => 'slug', 'terms' => $term, ) ); } $q= new WP_Query($args); wp_reset_postdata(); ?> <div class="theme_blog_wrap"> <div class="theme_blog_list theme_ajax_load_post" data-post_type="<?php echo $type;?>" data-per_page="<?php echo $count;?>" data-template ="<?php echo $template;?>" data-paged="1" > <div class="theme_blog_posts type-<?php echo $type;?> <?php if($layout == "masonry"){ echo "list_masonry"; } ?>"></div> </div> <div class="theme_load_more_wrap"> <div class="theme_load_more_posts"> Load more </div> </div> </div> <script> jQuery(function($) { var canBeLoaded = true; var ajaxurl = '<?php echo admin_url("admin-ajax.php"); ?>'; var this_container = $(".theme_blog_list"); $(window).on('load', function() { theme_load_post_data(); }); $(".theme_load_more_posts").click(function(){ theme_load_post_data(); }) function theme_load_post_data(){ if(canBeLoaded){ var per_pages = parseInt(this_container.attr("data-per_page")); var paged = parseInt(this_container.attr("data-paged")); var post_type= this_container.attr("data-post_type"); var template = this_container.attr("data-template"); var offset = per_pages*paged; var total = <?php echo $q->found_posts;?>; console.log(total); if( offset >= total ) { //$(".theme_load_more_posts").remove(); $(".theme_load_more_wrap").fadeOut(300); } var data = { 'action': 'theme_blog_ajax', 'per_page' : per_pages, 'paged': paged, 'offset': offset, 'post_type' : post_type, template : template, author : '<?php echo $author;?>', tax : "<?php echo $tax;?>", term : "<?php echo $term;?>" }; $.ajax({ url : ajaxurl, data:data, type:'POST', async: true, beforeSend: function( xhr ){ canBeLoaded = false; //$(".ajax_loader").show(); //$(".theme_blog_wrap").addClass("loading"); }, success:function(response){ //this_container.find(".theme_blog_posts").append(response).fadeIn(1000); $(response).hide().appendTo(this_container.find(".theme_blog_posts")).fadeIn(300); if(response !='' ){ paged++; this_container.attr("data-paged",paged); canBeLoaded = true; } //$(".ajax_loader").hide(); //$(".theme_blog_wrap").removeClass("loading"); } }); } } }) </script> <?php return ob_get_clean(); } add_shortcode("theme_blog_list", "theme_blog_list"); add_action('wp_ajax_theme_blog_ajax', 'theme_blog_ajax_callback'); add_action('wp_ajax_nopriv_theme_blog_ajax', 'theme_blog_ajax_callback'); function theme_blog_ajax_callback(){ $paged = $_POST['paged']; $per_page = $_POST['per_page']; $offset = $_POST['offset']; $post_type = $_POST['post_type']; $template = $_POST["template"]; $author = $_POST["author"]; $tax = $_POST["tax"]; $term = $_POST["term"]; $args = array( 'post_type' => $post_type, 'posts_per_page' => $per_page, 'paged' => $paged, 'post_status' => 'publish', 'post__not_in' => get_option( 'sticky_posts' ) ); if($author){ $args['author'] = $author; } if($tax && $term){ $args["tax_query"] = array( array( 'taxonomy' => $tax, 'field' => 'slug', 'terms' => $term, ) ); } $q = new WP_Query( $args ); if ( $q->have_posts() ) { while ( $q->have_posts() ) { $q->the_post(); get_template_part('templates/loop', $template ); } wp_reset_postdata(); } wp_die(); } function theme_sticky_post(){ $args = array( "post_type" => "post", "post_status" => "publish", 'post__in' => get_option( 'sticky_posts' ) ); $q = new WP_Query($args); ob_start(); if($q->have_posts()){ while($q->have_posts()){ $q->the_post(); get_template_part('templates/loop', "sticky" ); } } wp_reset_postdata(); return ob_get_clean(); } add_shortcode("theme_sticky_post", "theme_sticky_post"); function theme_related(){ $args = array( "post_type" => "post", "post_status" => "publish", "posts_per_page" => 3, 'post__not_in' => array(get_the_ID()), 'category__in' => wp_get_post_categories(get_the_ID()) ); $q = new WP_Query($args); ob_start(); if($q->have_posts()){ echo '<div class="theme_blog_posts ">'; while($q->have_posts()){ $q->the_post(); get_template_part('templates/loop', "blog" ); } echo '</div>'; } wp_reset_postdata(); return ob_get_clean(); } add_shortcode("theme_related", "theme_related"); /** * * Post Listing Shortcode End * */ function fancy_enqueue(){ if( ! is_front_page() ){ wp_enqueue_style( "fancy_css", get_stylesheet_directory_uri() . '/css/fancybox.css', [], '4.0', "all" ); wp_enqueue_script( "fancy_js", get_stylesheet_directory_uri() . '/js/fancybox.umd.js', ["jquery"], '4.0', ["in_footer" => true] ); } } add_action('wp_enqueue_scripts', 'fancy_enqueue');
Save File
Cancel