WordPress.org

Forums

[resolved] My pagination doesn't work correctly (15 posts)

  1. Raider000
    Member
    Posted 1 year ago #

    Hi,

    I use the function below to activate the pagination for my custom post type:

    function usePermalinks($weiter, $zurueck) {
    	$number_posts = get_option('posts_per_page');
    	$paged = get_query_var('paged');
    	$category = "showposts=".$posts;
    	query_posts($category."&paged=$paged&showposts=$number_posts");
    
    	if ($paged > 0) {
    		if (get_next_posts_link ()) {
    			echo '<br /><div class="pagination">' . next_posts_link($weiter) . '</div>';
    		}
    
    		if (get_previous_posts_link ()) {
    			echo '<br /><div class="pagination">' . previous_posts_link ( $zurueck) . '</div>';
    		}
    	}
    	else  {
    		echo 'There is no pagination for your.';
    	}
    }

    I use my own theme and when I put the function usePermalinks("Weiter", "Zurück"), it echos the else statement "There is no pagination for your.".

    How can I fix this?

  2. Raider000
    Member
    Posted 1 year ago #

    I figured out that get_next_posts_link () is always NULL. But why?

  3. kevin heath
    Member
    Posted 1 year ago #

    hi
    you're not calling the paged variable correctly.

    This is taken from the codex for pagination

    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    query_posts('posts_per_page=3&paged=' . $paged);
    ?>

    http://codex.wordpress.org/Pagination#Adding_the_.22paged.22_parameter_to_a_query

  4. Raider000
    Member
    Posted 1 year ago #

    Hi

    Thanks for this. I tried to build it like that but it won't work for me somehow.

    Here is my full code, maybe you see what's wrong.

    function usePermalinks($weiter, $zurueck) {
    	global $paged;
    	$number_posts = get_option('posts_per_page');
    	$paged = get_query_var('paged');
    	$category = "showposts=".$posts;
    	query_posts($category."&paged=$paged&showposts=$number_posts");
    	$paged = get_query_var('paged') ? get_query_var('paged') : 1;
    
    	if ($paged > 0) {
    	echo var_dump(next_posts_link()) ;
    		if (get_next_posts_link()) {
    			echo '<br /><div class="pagination">' . next_posts_link($weiter) . '</div>';
    		}
    
    		if (get_previous_posts_link()) {
    			echo '<br /><div class="pagination">' . previous_posts_link($zurueck) . '</div>';
    		}
    	}
    	else  {
    		echo '<br /><a class="blog_link" href="#" onclick="history.go(-1);return false;";>Back to the list</a>';
    	}
    }
    
    function showAllNews() {
    	$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args = array(
    			'posts_per_page' => 6,
    			'post_type' => 'news',
    			'orderby' => 'date',
    			'order' => 'DESC',
    			'paged' => $paged
    	);
    	$loop = new WP_Query( $args );
    	while ( $loop->have_posts() ) : $loop->the_post();
    	?>
    	<div class="span3 news-container left-align">
    		<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(); ?>">Weiterlesen</a>
    	</div>
    
    	<?php
    	endwhile;
    	usePermalinks("Vor", "Zurück"); // This won't work
    
    	next_posts_link('Older Entries'); // This won't work
        previous_posts_link('Newer Entries'); // This won't work
    
    	wp_query_reset();
    }
    add_shortcode('showAllNews', 'showAllNews');

    The shortcode I use on a page of my Custom Post Type.

  5. kevin heath
    Member
    Posted 1 year ago #

    Hi

    Your not building your query correctly for pagination.

    You have
    ."&paged=$paged&showposts=$number_posts");

    you need to bring in the variable $paged as php so

    ."&paged=' . $paged . '&showposts=$number_posts"`

  6. kevin heath
    Member
    Posted 1 year ago #

    sorry you also need to do the same with the $number_posts variable.

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    Also see how you use next_posts_link with a custom query (add another parameter):
    http://codex.wordpress.org/Function_Reference/next_posts_link#Usage_when_querying_the_loop_with_WP_Query

    next_posts_link( 'Older Entries', $loop->max_num_pages );
  8. Grafikz
    Member
    Posted 1 year ago #

    @keesiemeijer

    are you well willing to take a look for me at this post?

    http://wordpress.org/support/topic/warning-missing-argument-2-for-wpdbprepare-20?replies=1#post-4581293

  9. Raider000
    Member
    Posted 1 year ago #

    I have tried it like this:
    query_posts($category."&paged=".$paged."&showposts=".$number_posts);
    Is this correct?
    It's still not doing anything.

  10. Raider000
    Member
    Posted 1 year ago #

    I have also tried this code:

    function showAllNews() {
    /**
     * Displays the Pagination in Custom loop
     *
     */
    
    get_header();
    $temp = $wp_query; //save old query
    $wp_query= null; //clear $wp_query
    
    //The query
    $wp_query = new WP_Query();
    $wp_query->query( array( 'post_type' =>'news', 'posts_per_page'=>3, 'paged' => $paged ) );
    //The loop
    while ($wp_query->have_posts()): $wp_query->the_post(); 
    
         the_title();
    
    endwhile;
    
    //Pagination part
    if(function_exists('wp_pagenavi') ) {
         wp_pagenavi(); //function call for plugin pagination( wp pagenavi plugin)
    }else{ ?>
         <ul class="pagination">
              <li class="previous"><?php next_posts_link('Previous'); ?></li>
    	  <li class="next"><?php previous_posts_link('Next'); ?></li>
         </ul>
    <?php
    }//endif
    wp_reset_postdata();
    $wp_query = null; //Reset the normal query
    $wp_query = $temp;//Restore the query
    
    get_footer();
    }

    I get the title of my custom pages but here it won't show me the pagination as well.

    This code I just copy/pasted from http://devotepress.com/coding/wordpress-custom-loop-pagination/

    I didn't change much and I don't really see why it won't work. I think I visited all google sites to find a solution for this!

  11. kevin heath
    Member
    Posted 1 year ago #

    The last time I had an issue with pagination I had to go global with the query. May be you could look at doing something similar. The code below is from a plugin I developed to show blog posts within a member profile for WP Mingle. But you should be able to follow it and adapt as needed.

    // The Query
    
    global $wp_query;
    
    $paged= (get_query_var('paged')) ? get_query_var('paged') : 1;
    $mblogn = get_option('mblog_number'); //this is number of posts to display
    $mblogcat = get_option('mblog_cats'); //this is the categories to take posts from.
    $wp_query = new WP_Query( 'author='. $user_id .'&paged='. $paged .'&showposts='. $mblogn .'&cat='. $mblogcat .'' );
    
    // The Loop
    if ( $wp_query->have_posts() ) {
    	while ( $wp_query->have_posts() ) {
    		$wp_query->the_post(); ?>
    		 <li>
                <?php the_post_thumbnail('thumbnail'); ?> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link: <?php the_title(); ?>">
               <div id="mblogtitle"> <?php the_title(); ?></a></div>
                <div id="mblogdate"><?php the_time('d M Y'); ?> </div>
    
    			<div id="mblogexc"><?php the_excerpt(); ?></div>
            </li>
    		<?php
    	}
    	?>
    
    				<div id="nav-below" class="navigation">
    					<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
    
    					<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
    				</div><!-- #nav-below -->
    
    <?php
    
    } else {
    echo 'No posts by this member.';
    }

    obviously you'll need to set post type for your situation and reset the query etc but the important thing to remember is to set the
    global $wp_query otherwise the pagination does not work.

    not sure thee's much else I can contribute sorry.

    Kevin

  12. Raider000
    Member
    Posted 1 year ago #

    That's perfect, I'll see what I can find out. Thank you!!

  13. Raider000
    Member
    Posted 1 year ago #

    Hi again,

    I made it now! This works:

    function showAllNews() {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$args = array(
    			'posts_per_page' => get_option('posts_per_page'),
    			'post_type' => 'news',
    			'orderby' => 'date',
    			'order' => 'DESC',
    			'paged' => $paged
    	);
    	$loop = new WP_Query( $args );
    	while ( $loop->have_posts() ) : $loop->the_post();
    	?>
    	<div class="span5 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(); ?>">Weiterlesen</a>
    	</div>
    
    	<?php
    	endwhile;
    	previous_posts_link ( '<span class="meta-nav">&larr;</span>Zurück', $loop->max_num_pages );
    	echo '&nbsp;&nbsp;&nbsp;';
    	next_posts_link( 'Weiter<span class="meta-nav">&rarr;</span>', $loop->max_num_pages );
    }

    With next and previous_posts_link I have only 2 links. Is there a way I could have numbers for my pagination?

  14. keesiemeijer
    moderator
    Posted 1 year ago #

    See this example: https://codex.wordpress.org/Function_Reference/paginate_links#Examples

    change this:

    'total' => $wp_query->max_num_pages

    to this:

    'total' => $loop->max_num_pages
  15. Raider000
    Member
    Posted 1 year ago #

    Oh wow, this works great! Thank you so much!!!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags