• Hi,

    I am using this code below to display a preview of my posts:

    query_posts('cat=1&posts_per_page=1');
    	while ( have_posts() ) : the_post();
    	?>
    	<div class="span12 news-container left-align listnews">
    		<h3><?php the_title(); ?></h3>
    		<p>
    			<?php echo substr(get_the_content(), 0, 180); ?>
    			<?php if (strlen(get_the_content()) > 180) { echo " ..."; } ?>
    		</p>
    		<a class="news-button" href="<?php the_permalink(); ?>">Read more...</a>
    	</div>
    
    	<?php
    	endwhile;
    	wp_reset_query();

    I now need to display a pagination to this. I’ve been using a pagination for my custom post types which worked perfectly but the same one won’t work for normal posts. That’s the code:

    $big = 999999999;
    	echo '<div class="pagination">';
    	echo paginate_links( array(
    		'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    		'format' => '?paged=%#%',
    		'current' => max( 1, get_query_var('paged') ),
    		'total' => $loop->max_num_pages
    	) );
    	echo '</div>';

    Is there something I could change on this to make it also work for my posts?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    See the example here:
    https://codex.www.ads-software.com/Function_Reference/paginate_links#Basic_Example

    Add global $wp_query; before the function and change this:

    'total' => $loop->max_num_pages

    to this:

    'total' => $wp_query->max_num_pages

    And add the paged variable to the query:
    https://codex.www.ads-software.com/Pagination#Adding_the_.22paged.22_parameter_to_a_query

    Thread Starter Raider000

    (@raider000)

    Uhm, I did what you said but it doesn’t show me anything. I’m sure I was doing something wrong. Maybe you want to take a look at my current code:

    global $wp_query;
    function showAllNews() {
    	query_posts('cat=1&posts_per_page=1');
    	while ( have_posts() ) : the_post();
    	?>
    	<div class="span12 news-container left-align listnews">
    		<h3><?php the_title(); ?></h3>
    		<p>
    			<?php echo substr(get_the_content(), 0, 180); ?>
    			<?php if (strlen(get_the_content()) > 180) { echo " ..."; } ?>
    		</p>
    		<a class="news-button" href="<?php the_permalink(); ?>">Read more...</a>
    	</div>
    
    	<?php
    	endwhile;
    	wp_reset_query();
    
    	$big = 999999999;
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	echo '<div class="pagination">';
    	echo paginate_links( array(
    		'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
    	) );
    	echo '</div>';
    }
    add_shortcode('showAllNews', 'showAllNews');
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to display pagination for posts?’ is closed to new replies.