Today, we are going to share an interesting code snippet about the “more” tag problem in WordPress pages. “more” tag works perfectly on the Home page which shows the latest posts but it fails to work in the custom page templates. Here is a simple code snippet which fix this issue instantly.
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $args = array( 'post_type' => 'post', 'paged' => $paged ); $custom_query = new WP_Query( $args ); if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); /** * The code must be inserted ahead of the call the_content * but AFTER the_post() */ global $more; $more = 0; /** End code snippet */ get_template_part( 'content' ); endwhile; wp_reset_postdata(); endif;
For your understanding, I have written the above full code to fetch the posts in a custom page template. In order to work the “more” tag perfectly in our custom page template, we have to declare it global and set to 0. Following points should be consider in your code,
- The code must be inserted ahead of the call the_content.
- The code must be inserted AFTER the_post().