Support » Fixing WordPress » Can't get pagination links to work

  • MeindertHH

    (@meinderthh)


    Hi everyone!

    I’m building a blog and it almost works the way I want (so that’s great). The only thing that doesn’t work is the pagination section. I would like it to look like this:

    « Prev 1 … 3 4 5 6 7 … 9 Next »

    (Anything close would do fine.)

    But how?

    This is what I got thus far.

    <?php
    // WP_Query arguments
    $args = array (
    	'post_status'            => 'publish',
    	'pagination'             => true,
    	'posts_per_page'         => '5',
    	'order'                  => 'DESC',
    	'orderby'                => 'date',
        'paged'                  => $paged,
        'base'                   => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    	'format'                 => '?paged=%#%',
    	'current'                => max( 1, get_query_var('paged') ),
    	'total'                  => $wp_query->max_num_pages,
        'before_page_number'     => '<span>Page </span>'
    );
    
    // The Query
    $query = new WP_Query( $args );
    $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
    $big = 999999999; // need an unlikely integer
    
    // The Loop
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post();
            $cats = '';
            foreach( get_the_category( $recent["ID"] ) as $cat ) { $cats .= ' ' . $cat->slug; }
    		echo '<header><h4>' .   get_the_title() . '</h4><p>'. get_the_date() . ' - <span class="icon ' .  $cats .'"></span></p></header><p>'. get_the_content() . '</p>';
        }
    } else {
        echo paginate_links();
    }
    
    // Restore original Post Data
    wp_reset_postdata();
    
    ?>

    I am not very familiar with php, so I hope anyone of you can find a few minutes to help me out here.

    Thanks very much in advance!

    Meindert

Viewing 2 replies - 1 through 2 (of 2 total)
  • MeindertHH

    (@meinderthh)

    I maybe got a little bit closer…

    <?php
    // WP_Query arguments
    $args = array (
    	'pagination'             => true,
    	'posts_per_page'         => '5',
    	'order'                  => 'DESC',
    	'orderby'                => 'date',
    );
    // the query
    $the_query = new WP_Query( $args );
    ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
    	<!-- pagination here -->
    
    	<!-- the loop -->
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<p>Title and content here</p>
    	<?php endwhile; ?>
    	<!-- end of the loop -->
    
    	<!-- pagination here -->
    <?php
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
    	'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ),
    	'format' => '?paged=%#%',
    	'current' => max( 1, get_query_var('paged') ),
    	'total' => $the_query->max_num_pages
    ) );
    ?>
    	<?php wp_reset_postdata(); ?>
    
    <?php else : ?>
    	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>

    The problem is that esc_url( get_pagenum_link( $big ) ) sends me to my wordpress page. I want people to stay on my own domain. So ‘next page’ should redirect to mydomain.com/index.php?page=x.

    Anyone?

    Thanks!

    MeindertHH

    (@meinderthh)

    To make it a bit more clear I made this example page.

    Code used:

    <?php require('wp/wp-blog-header.php');?>
    
    <!DOCTYPE HTML>
    <html>
    	<head>
        </head>
        <body>
    <?php
    // WP_Query arguments
    $args = array (
    	'pagination'             => true,
    	'posts_per_page'         => '3',
    	'order'                  => 'DESC',
    	'orderby'                => 'date',
    );
    // the query
    $the_query = new WP_Query( $args );
    ?>
    
    <?php if ( $the_query->have_posts() ) : ?>
    
    	<!-- pagination here -->
    
    	<!-- the loop -->
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post();         $cats = '';
    foreach( get_the_category( $recent["ID"] ) as $cat ) { $cats .= ' ' . $cat->slug; } ?>
    		<header><h4><?php   the_title(); ?></h4><p><?php the_date(); ?></p></header><p><?php the_content(); ?></p>
    	<?php endwhile; ?>
    	<!-- end of the loop -->
    
    	<!-- pagination here -->
    <?php
    $big = 999999999; // need an unlikely integer
    
    echo paginate_links( array(
    	'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    	'format' => '?paged=%#%',
    	'current' => max( 1, get_query_var('paged') ),
    	'total' => $the_query->max_num_pages
    ) );
    ?>
    	<?php wp_reset_postdata(); ?>
    
    <?php else : ?>
    	<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    <?php endif; ?>
        </body></html>

    I am very grateful if someone would help me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Can't get pagination links to work’ is closed to new replies.