• I took over a website that was custom programmed and the pagination is broken on it. I think it may be the theme that is outdated but the client is asking for me to fix it and I’m not sure what to do.

    Here is the PHP code of the index page:

    <?php 
    get_header(); ?>
    
    <h1 style="margin-left:20px;margin-top:5px;color:#2B4E74;"> Latest News</h1><br />
    
    <?php $recent = new WP_Query("posts_per_page=9"); while($recent->have_posts()) : $recent->the_post();?>
    
    <div class="post">
    	<div class="r">
    
    		<h3><a>"><?php the_title(); ?></a></h3><p><strong style="color:#2B4E74;">Category:</strong> <?php the_category(', ') ?></p>
    		<div class="noimage"><?php the_content(''); ?></div>
    		<div class="details" style="float:right;"><p><a>">Read More &raquo;</a></p></div>
    	</div>
    <div class="break"></div></div>
    
    <?php endwhile; ?><br /><?php next_posts_link('&laquo; Older Entries'); ?>
                    </div>
    <?php get_sidebar(); get_footer(); ?>

    I don’t know PHP and all of the queries so if someone has insight into what needs to be adjusted to fix this I’d appreciate it.

    Thanks,

    Daniel Boswell

    • This topic was modified 5 years, 12 months ago by Jan Dembowski. Reason: Formatting

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi, Daniel.

    I noticed in the code

    <?php $recent = new WP_Query(“posts_per_page=9″);

    Isn’t handing WP_Query an argument in the right format.

    I believe it should be

    <?php $recent = new WP_Query( array( 'posts_per_page' => 9 ) );

    Based on what I read from https://developer.ww.wp.xz.cn/reference/classes/wp_query/#pagination-parameters

    Typically the theme doesn’t break the pagination in its normal Loop; it’s the extra stuff like widgets or other loops for data that are coded incorrectly (affecting global variables and not doing a reset).

    Try using the Health Check plugin in Troubleshoot mode, which disables plugins (and theme) for your user only, to isolate the source of the problem.
    If it is the theme, you can switch themes.
    If it’s a plugin, ask the plugin author to fix it or just find a replacement.

    Thread Starter dcboswell

    (@dcboswell)

    Yeah the problem is that it shows the pagination in the URL but it never shows anything beyond the first 9 articles on the site.

    I don’t know if that is something from the PHP code or not.

    Well, everything on the page and the URL is from PHP code. The trick is to identify which code (core WP, plugin, theme). Use Health Check troubleshooting to isolate it, and then you can remedy it.

    Moderator bcworkz

    (@bcworkz)

    WP_Query will not pick up the URL paged parameter on its own. You need to get it from the main query object and pass it to the custom WP_Query. That’s not optimal though. A custom query for the main content on a theme’s index.php template is abusing the purpose of the template. index.php should contain a standard loop. It’s similar but different to what you have. Refer to the index.php of the twentytwenty theme for an example (the part that starts with if ( have_posts() )). What’s within the loop is up to you, but how it’s driven with have_posts() and the_post() (the global functions, not $recent object methods) is what we’re interested in

    If you want to modify the main query of a particular page to have different posts per page than that set in settings, it ought to be done through the “pre_get_posts” action. By utilizing the main query as intended, pagination will work on its own without any fuss.

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Broken Pagination’ is closed to new replies.