如何应用加载更多来获取 WordPress 模板中的自定义帖子

在我的网站中,我使用 get posts 获取自定义帖子类型的帖子。但是一次获取所有帖子,但在我的模板中,我想显示每页剩余的 x 个帖子,我想通过使用加载更多按钮加载。


       <?php 


         $id = url_to_postid( $current_url );

         $queried_post = get_post($id); 

         $post_slug = $queried_post->post_name;


         $page_number = (get_query_var('paged')) ? 

              get_query_var('paged') : 1;

           $page_link =  get_pagenum_link(9999999999);

           $term = get_term_by('slug', $post_slug, 

                     'tax_business_listing'); 

           $cat_term_id = $term->term_id;

               $args = array(


                'posts_per_page' =>3,

                'paged'=>$page_number,

                'order'            => 'ASC',

                'post_type'        => 'business',

                'post_status'      => 'publish',

                'offset'           =>  0,

                'orderby'          => 'ASC',

                 'tax_query' => array(

        array(

            'taxonomy' => 'tax_business_listing',

            'field' => 'term_id',

            'terms' => $cat_term_id,

        )

    ),

             ); 


         $posts =   get_posts( $args );

          $loop = new WP_Query( $args );

          $count = !empty($loop->posts)?count($loop->posts):0;


          $max_page = $loop->max_num_pages;


          $i=0;

         if(!empty($posts)){

         foreach ($posts as $post): 

          $i++;

          $postName = $post->post_title;

          $exp = $post->post_excerpt;

          $thumb_id = get_post_thumbnail_id();

          $thumb_url_array = wp_get_attachment_image_src($thumb_id, 

                               'thumbnail-size', true);

             $thumb_url = $thumb_url_array[0];


通过包含上面的代码,我在点击加载更多按钮后获得第一个帖子 3 次。我想加载剩余的帖子。请帮助我获得预期的结果。非常感谢任何帮助..


HUWWW
浏览 131回答 2
2回答

皈依舞

您的代码中可能存在一些问题,请尝试以下代码。请根据您的代码更改条件并检查它是否有效。你的模板.php<div id="ajax-posts" class="row"><?php get_header(); ?>&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; $args = array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'post_type' => 'business',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'paged'&nbsp; &nbsp; => 1,&nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $loop = new WP_Query($args);&nbsp; &nbsp; &nbsp; &nbsp; while ($loop->have_posts()) : $loop->the_post();&nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp;<div class="content">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <h1><?php the_title(); ?></h1>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><?php the_content(); ?></p>&nbsp; &nbsp; &nbsp;</div>&nbsp; &nbsp; &nbsp;<?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; endwhile;&nbsp; &nbsp; wp_reset_postdata();&nbsp; &nbsp; &nbsp;?></div>&nbsp;<p class="para"></p><button id="more_posts">Load More</button>脚本.jsvar pageNumber = 2;jQuery(function($) {$('body').on('click', '#more_posts', function load_posts(){&nbsp; &nbsp; var str = '&pageNumber=' + pageNumber + '&action=more_post_ajax';&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; dataType: "html",&nbsp; &nbsp; &nbsp; &nbsp; url: ajax_posts.ajaxurl,&nbsp; &nbsp; &nbsp; &nbsp; data: str,&nbsp; &nbsp; &nbsp; &nbsp; success: function(data){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var $data = $(data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($data.length){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#ajax-posts").append($data);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#more_posts").attr("disabled",false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $(".para").text("No More Posts");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#more_posts").attr("disabled",true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pageNumber++;&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; error : function(jqXHR, textStatus, errorThrown) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $loader.html(jqXHR + " :: " + textStatus + " :: " + errorThrown);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});});函数.php<?phpadd_action('wp_enqueue_scripts', 'myscripts');function myscripts() {&nbsp;wp_register_script('functions', get_template_directory_uri() .'/script.js');wp_localize_script( 'functions', 'ajax_posts', array('ajaxurl' => admin_url( 'admin-ajax.php' ),));&nbsp;wp_enqueue_script('functions');}if (isset($_REQUEST['posts_per_page'])) {$pageCount = $_REQUEST['posts_per_page'];$wp_query->set("posts_per_page", $pageCount);$wp_query->get_posts();}add_action('wp_ajax_more_post_ajax', 'more_post_ajax');add_action('wp_ajax_nopriv_more_post_ajax', 'more_post_ajax');function more_post_ajax(){$page = (isset($_POST['pageNumber'])) ? $_POST['pageNumber'] : 0;$args = array(&nbsp; &nbsp; 'post_type' => 'competition',&nbsp; &nbsp; 'paged'&nbsp; &nbsp; => $page,);$loop = new WP_Query($args);if ($loop -> have_posts()) :&nbsp; while ($loop -> have_posts()) : $loop -> the_post();&nbsp; &nbsp; ?>&nbsp; &nbsp; &nbsp; &nbsp; <h2><?php the_title() ?></h2>&nbsp; &nbsp; &nbsp; &nbsp; <?php the_excerpt() ?><?phpendwhile;endif;wp_reset_postdata();}?>
打开App,查看更多内容
随时随地看视频慕课网APP