[wp-trac] [WordPress Trac] #59043: the_excerpt() function return excerpt with different length in page load and ajax request on WordPress 6.3

WordPress Trac noreply at wordpress.org
Thu Aug 10 12:38:13 UTC 2023


#59043: the_excerpt() function return excerpt with different length in page load
and ajax request on WordPress 6.3
-------------------------------------------------+-------------------------
 Reporter:  sarathlal                            |       Owner:  (none)
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
                                                 |  Review
Component:  Posts, Post Types                    |     Version:  6.3
 Severity:  normal                               |  Resolution:
 Keywords:  has-patch needs-testing needs-       |     Focuses:
  testing-info                                   |
-------------------------------------------------+-------------------------

Comment (by sarathlal):

 @poena Sure.

 Here are the custom code I'm using in my theme.

 *** Code in home page template.

 {{{#!php
 <?php
 $current_page = get_query_var('paged');
 $per_page = get_option('posts_per_page');
 $offset = $current_page > 0 ? $per_page * ($current_page-1) : 0;

 $blog_args = array(
     'post_type' => 'post',
     'post_status' => 'publish',
     'posts_per_page' => $per_page,
     'offset' => $offset,
     'order'=>'DESC',
     'orderby'=>'date',
 );
 $blogs_query = new WP_Query($blog_args);

 if ( $blogs_query->have_posts() ) { ?>
         <section class="py-3">
                 <div class="container">
                         <p class="section-title">Other Blogs</p>

                         <div id="th-blog-list" class="row posts-grid gx-5
 gy-4">
                                 <?php
                 while ($blogs_query->have_posts()) :
 $blogs_query->the_post();
                     $post_id   = get_the_ID();
                     ?>
                     <?php get_template_part('template-parts/post-card');
 ?>
                                 <?php endwhile; ?>
                         </div>

                         <?php
                         $data = array(
                                 'paged' => 2,
                                 '_wpnonce' => wp_create_nonce('load-
 posts'),
                         );
                         global $wp_query;
                         if(isset( $blogs_query->max_num_pages ) &&
 ($blogs_query->max_num_pages > 1)){ ?>
                                 <div class="row mb-4 mt-4">
                                         <div class="col-12 text-center">
                                                 <span class="spinner-
 border spinner-border-sm" style="display:none" id="btn-loader"
 role="status" aria-hidden="true"></span>
                                                 <?php
                                                 echo '<button
 type="button" class="btn btn-dark ps-4 pt-2 pe-4 pb-2" id="load-more" '.
 implode(' ', array_map(
                                                     function ($k, $v) {
 return "data-" . $k .'="'. htmlspecialchars($v) .'"'; },
                                                     array_keys($data),
 $data
                                                 )) .' />Show
 more</button>';
                                                 ?>
                                         </div>
                                 </div>
                         <?php } ?>

                 </div>
         </section>
 <?php } ?>
 <?php wp_reset_postdata(); ?>
 }}}


 *** Code in template-parts/post-card.php

 {{{#!php
 <?php
 /**
  * Template part for displaying blog post card
  *
  * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
  *
  */
 ?>

 <div class="col-sm-12 col-md-6">
     <div class="post-item">
             <a href="<?php echo get_permalink(get_the_ID()); ?>">
                     <?php echo get_the_post_thumbnail( get_the_ID(),
 'full', array( 'class' => 'f-img' ) ); ?>
             </a>
             <p class="title"><a href="<?php the_permalink(); ?>"><?php
 the_title(); ?></a></p>
             <p class="short-desc"><?php the_excerpt(); ?></p>
             <p class="author"><?php echo get_the_author(); ?> | <?php echo
 get_the_date('F j, Y', get_the_ID()); ?></p>
     </div>
 </div>
 }}}

 *** Code in functions.php for AJAX load more

 {{{#!php
 function th37t_load_more_posts() {
         $req = $_REQUEST;

         if(!isset($_REQUEST['_wpnonce'])){
                  die('stop');
         }

         if(!(wp_verify_nonce( $_REQUEST['_wpnonce'], 'load-posts'))){
                 die('stop');
         }

         unset($req["action"]);
         unset($req["_wpnonce"]);

         $per_page = get_option('posts_per_page');
         $default_data = array(
                 'post_type' => 'post',
                 'posts_per_page' => $per_page,
                 'post_status' => 'publish',
                 'order'=>'DESC',
                 'orderby'=>'date',
         );
         $args = array_merge($default_data, $req);

         $ajaxposts = new WP_Query($args);

         $html = '';
         if($ajaxposts->have_posts()) {
                 while($ajaxposts->have_posts()) : $ajaxposts->the_post();
                     ob_start();
                     get_template_part('template-parts/post-card');
                     $html .= ob_get_contents();
                     ob_end_clean();

                 endwhile;
         }

         $next_page = false;
         $max_pages = isset($ajaxposts->max_num_pages) ?
 $ajaxposts->max_num_pages : false;

         $current_page = isset($_REQUEST['paged']) ? $_REQUEST['paged'] :
 false;

         if( $current_page && $max_pages && $current_page < $max_pages ){
                 $next_page = $current_page + 1;
         }

         $data = array(
                 'html' => $html,
                 'next_page' => $next_page,
         );

         wp_send_json($data);
         exit;
 }
 add_action('wp_ajax_th_load_more_posts', 'th37t_load_more_posts');
 add_action('wp_ajax_nopriv_th_load_more_posts', 'th37t_load_more_posts');

 }}}


 *** Jquery code for "Load more" button

 {{{
 (function($){
         $('#load-more').on('click', function() {
                 $('#btn-loader').show();
                 $('#load-more').attr("disabled", true);
                 var data = $(this).data();

                 data['action'] = 'th_load_more_posts';

                 $.ajax({
                         type: 'POST',
                         url: "<?php echo admin_url('admin-ajax.php'); ?>",
                         dataType: 'JSON',
                         data: data,
                         success: function (res) {
                                 $('#btn-loader').hide();
                                 if(res.html){
                                         $('#th-blog-
 list').append(res.html);
                                 }

                                 if(res.next_page){
                                         $('#load-more').data('paged',
 res.next_page);
                                         $('#load-more').attr("disabled",
 false);
                                 }else{
                                         $('#load-more').attr("disabled",
 true);
                                 }
                         }
                 });
         });
 })(jQuery);
 }}}

 Now on initial page load, the excerpt length will be 50 words if there are
 no any filter. But when I click on "Load More" button, the length will be
 100 words.

 I'm little confused about why the code in WordPress block affect core
 functionality?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59043#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list