Next button not showing other posts in Wordpress - php

In my wordpress I have many posts, every page I am showing 5 posts.
Bottom of my page I have next and prev button. When I click one the next button it will go to /page/2/ link but this page title is showing Page not found. And it's not showing other posts in page 2.
My next and prev code :
<div class="prev-next-btn">
<?php next_posts_link( __( 'next', 'themename' ) ); ?>
</div>
<div class="prev-next-btn">
<?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
</div>
My index.php code :
<div class="center-content">
<ul>
<?php query_posts('posts_per_page=5'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<div class="date">In
<?php
$category = get_the_category();
if(!empty($category))
echo ''.$category[0]->cat_name.'';
?>
- <?php the_time('d M, Y') ?>
</div>
<!--<div class="auther">by <?php the_author(); ?> <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
<div class="title clear-both"><h2><?php the_title(); ?></h2></div>
<div class="details"><p><?php the_excerpt(); ?></p></div>
<div class="readmore">Read more</div>
<br>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="pagination">
<?php
if(function_exists('wp_pagenavi')) {
wp_pagenavi();
}
else {
?>
<div class="prev-next-btn">
<?php next_posts_link( __( 'next', 'themename' ) ); ?>
</div>
<div class="prev-next-btn">
<?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
</div>
<?php } ?>
</div>
<?php else : ?>
404 Nothing here. Sorry.
<?php endif; ?>
</div>

You do have a few issues here
NEVER EVER use query_posts. It is slow, reruns queries, breaks and fails silently with pagination and worse of all, it breaks the main query object. If you break the main query object, you break page functions. So, please never use query_posts. Mke if it never existed
You have replaced the main query loop with a custom query, which you must not do. If you need to show a different amount of posts on a particular page ( not on page templates and a static front page though as this will not work there ), then use pre_get_posts. You need to go and read how helpful that action is and how to use it
Remove this line
<?php query_posts('posts_per_page=5'); ?>
Then add the following in your functions file
add_action( 'pre_get_posts', function ( $query )
{
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 5 );
}
});

<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=1&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Next', $the_query->max_num_pages );
previous_posts_link( 'Previous' );
?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php endif; ?>

Just add below code: I added 'paged' variable and set in next_posts_link() and previous_posts_link()..please see below code:
<div class="center-content">
<ul>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('posts_per_page'=>5,'paged'=>$paged )); ?>
<?php if (have_posts()) : while (have_posts()) :the_post(); ?>
<li>
<div class="date">In
<?php
$category = get_the_category();
if(!empty($category))
echo ''.$category[0]->cat_name.'';
?>
- <?php the_time('d M, Y') ?>
</div>
<!--<div class="auther">by <?php the_author(); ?> <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
<div class="title clear-both"><h2><?php the_title(); ?></h2></div>
<div class="details"><p><?php the_excerpt(); ?></p></div>
<div class="readmore">Read more</div>
<br>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="pagination">
<?php
global $wp_query;
if(function_exists('wp_pagenavi')) {
wp_pagenavi();
}
else { echo 'test';
?>
<div class="prev-next-btn">
<?php echo next_posts_link( __( 'next', 'themename' ) , $wp_query->max_num_pages ); ?>
</div>
<div class="prev-next-btn">
<?php echo previous_posts_link( __( 'prev', 'themename' ) , $wp_query->max_num_pages ); ?>
</div>
<?php } ?>
</div>
<?php else : ?>
404 Nothing here. Sorry.
<?php endif; ?>
</div>

Related

Show more post stanleywp theme WordPress

I'm using stanleywp theme for WordPress to display some post, actually I've inserted 20 posts, but it shows only 10posts.
This is what I did:
Portfolio -> Portfolio categories -> I added "brands" category
Portfolio -> add new -> and I've added 20 posts and set "brands" category
when I go to .../portfolio-category/brands/ I should see all my 20 posts, but I see only 10.
How can I solve that?
This is taxonomy-portfolio_cats.php:
<?php
/**
* #package WordPress
*/
?>
<?php get_header(); ?>
<?php if (have_posts()) : ?>
<div class="container pt">
<div class="row mt">
<div class="col-lg-6 col-lg-offset-3 centered">
<h1><?php echo single_term_title(); ?></h1>
<hr>
<?php if(category_description()) { ?>
<?php echo category_description( ); ?>
<?php } ?>
</div>
</div>
<div class="row mt centered">
<?php $cont = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<div class="col-lg-4">
<?php if ( has_post_thumbnail()) : ?>
<a class="zoom green" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<?php if(bi_get_data('project_title', '5')) {?>
<p><?php the_title(); ?></p>
<?php } ?>
</div> <!-- /col -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</div>
<?php endif; ?>
<?php get_footer(); ?>
I think it's due to the default pagination (10 posts per page): you could increase the value on Settings > Reading > Blog pages show at most, or insert the pagination after <?php wp_reset_query(); ?>, something like:
<?php
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'your-textdomain' ),
'next_text' => __( 'Next page', 'your-textdomain' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'your-textdomain' ) . ' </span>',
) );
?>
Or if you want to change the number of posts per page only for that taxonomy archive you can use the pre_get_posts action:
function portfolio_cats_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( is_tax( 'portfolio_cats' ) ) {
// Display all posts in one page
$query->set( 'posts_per_page', -1 );
}
}
add_action( 'pre_get_posts', 'portfolio_cats_posts_per_page', 1 );

Have I made a mistake with my WordPress pagination?

The pagination on a site I am working on is not working; I really can't work out why as it is the same as the other templates within the site. I am wondering if there is a problem with the loop, perhaps where I have specified what category I want it to pull.
This is the page I am having trouble with:
<div class="container content newspage">
<div class="two_third newsarticles">
<?php /* Start the Loop */ ?>
<?php $catquery = new WP_Query( 'cat=388' ); while($catquery->have_posts()) : $catquery->the_post(); ?>
<?php get_template_part( 'content-archive', get_post_format() ); ?>
<?php endwhile; ?>
<nav id="nav-below">
<div class="nav-previous">
<?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→ </span>', 'anariel' ) ); ?>
</div>
</nav>
<!-- end nav-below -->
</div>
<!-- end two_third -->
<div class="sidebar">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Default Sidebar') ) : ?>
<?php endif; ?>
</div>
</div>
<!-- end container -->
<?php get_footer(); ?>
The template that does work correctly is here:
<div class="container content newspage">
<div class="two_third newsarticles">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<nav id="nav-below">
<div class="nav-previous">
<?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→ </span>', 'anariel' ) ); ?>
</div>
</nav>
<!-- end nav-below -->
<?php endif; ?>
</div>
<!-- end two_third -->
<aside>
<div class="one_third lastcolumn newssidebar">
<?php get_sidebar(); ?>
</div>
</aside>
</div>
<!-- end container -->
<?php get_footer(); ?>
I have tried copying this over into the template that doesn't work without success.
When you're using a custom query, you have to add the page number to your arguments.
<?php /* Start the Loop */ ?>
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$catquery = new WP_Query( array('cat' => 388, 'paged' => $paged) ); while($catquery->have_posts()) : $catquery->the_post(); ?>
You should also pass the number of pages to get_next_posts_link
next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ), $catquery->max_num_pages );

WP-pagination and excluding category from page

I'm trying to use WP pagination on post archive page but exclude posts from one category to be shown there.
When I add this to my code the page2,3,4... of the archive display the same first 10 posts:
<?php query_posts('cat=-4');?>
This is the whole code of my page template so I would be grateful for all your help:
<?php
/*
Template Name: Post archive
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="content col-md-9">
<div class="home-content">
<!-- Show posts -->
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php query_posts('cat=-4');?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
<div style="float:left; margin:1%;">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'thumbnail', array( 'class' => 'img-post')); // show featured image
}
?>
</div>
<h1 class="post-thumb"><?php the_title(); ?></h1>
<h4>Category: <?php the_category(', '); ?></h4>
<p><?php the_excerpt(); ?></p>
<hr style="margin-bottom:5%">
<?php endwhile; ?>
<!-- pagination -->
<div class="nav-previous alignleft" style="margin-top:-1%"><?php next_posts_link( 'See older posts' ); ?></div>
<div class="nav-next alignright" style="margin-top:-1%"><?php previous_posts_link( 'See newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
</div>
<div class="col-md-3 sidebar unstyled">
<?php dynamic_sidebar( 'home1' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'home2' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'articles1' ); ?>
</div>
</div>
</div>
</body>
</html>
<?php get_footer(); ?>
This code fixed my page:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat'=> -4,
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
Modifying to reflect OP's solution for benefit of future readers
Change query() slightly as shown below
$args = array(
'cat'=> -4,
'posts_per_page'=> 10,
'paged'=> $paged
);

Getting data twice on page in wordpress

I have my theme page.php as:
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<header class="entry-header">
<div class="hd"><?php the_title(); ?></div>
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="video"><?php the_post_thumbnail(); ?></div>
<?php endif; ?>
<div class="hd"><?php //the_title(); ?></div>
</header><!-- .entry-header -->
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<!-- .entry-content -->
<footer class="entry-meta">
<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<?php // comments_template( '', true ); ?>
<?php endwhile; ?>
And I have made three page in wordpress blog, image and news and i also assigned them category for each. now i have installed php-exec plugin . Now I am writing some php code in page editor to retrieve blog data...
It working fine but it fetching data twice and now got it is becouse of page.php .
So can i have some condition on page.php if i am trying to fetch some data by cotegory then page.php data would not be display...
here is my code which i applied on blog page editor
<?php if (query_posts('cat=63&showposts=5')) : ?>
<?php while (have_posts()) : the_post();
// do whatever you want
?>
<div class="gallery_views">
<div class="hd"><?php the_title(); ?></div>
<?php // get_template_part( 'content', get_post_format() ); ?>
<?php // cup_post_nav(); ?>
<?php the_post_thumbnail(); ?>
<?php comments_template(); ?>
<b><?php the_title(); ?></b>
</div>
<?php endwhile; ?>
<?php else : ?>
Thanks in advance..
<?php if (query_posts('cat=63&showposts=5')) : ?>
<?php while (have_posts()) : the_post();
// do whatever you want
?><div class="gallery_views">
<div class="hd"><?php the_title(); ?></div>
<?php // get_template_part( 'content', get_post_format() ); ?>
<?php // cup_post_nav(); ?>
<?php the_post_thumbnail(); ?>
<?php comments_template(); ?>
<b><?php the_title(); ?></div>
<?php
break;
endwhile;
?>
<?php else : ?>
Add a break in your while, it'll stop after the first loop.

How to create separate pagination for mutiple loops?

I have created two loops with pagination (first loop loops through CAT'S category and second loops through DOG'S category), but now I am stuck:(
The problem: After I click "Next entry" on my site (CAT'S category) it goes to second entry in that category BUT it also goes to my DOG'S category second entry (I don't want THAT!! ). It also happens vice versa...
What I like to do is this: I click on "Next Entry" on my CAT'S category and it goes only to next post in THAT category (CAT'S) but NOT to second post in my DOG'S category, or another way around: I click on "Next Entry" on my DOG'S category and it goes only to next post in THAT category (DOG'S) but NOT to second post in my CAT'S category .
Can someone help me please? I have asked for help on
wordpress.stackexchange.com a while ago but I didn't get any answer so I am asking question here.
Index php looks like this:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<div id="blog">
<?php
$args = array(
'category_name' => 'cats'
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_query($args . '&paged=' . $paged . '&cat=-3');
while( $the_query -> have_posts()) : $the_query -> the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_postdata();?>
<div class="navigation">
<div style="float:left;" class="alignleft"><?php previous_posts_link('« Previous Entries') ?></div>
<div style="float:right;" class="alignright"><?php next_posts_link('Next Entries »',$the_query->max_num_pages) ?></div>
</div>
</div>
<div id="blogs">
<?php
$args = array(
'category_name' => 'dogs'
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_query($args . '&paged=' . $paged . '&cat=-10');
while( $the_query -> have_posts()) : $the_query -> the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_postdata();?>
<div class="navigation">
<div style="float:left;" class="alignleft"><?php previous_posts_link('« Previous Entries') ?></div>
<div style="float:right;" class="alignright"><?php next_posts_link('Next Entries »',$the_query->max_num_pages) ?></div>
</div>
</div>
<?php get_footer(); ?>
You need 2 different paging values so add some new ones and rewrite rules which look for them (they're really just to make the urls neater looking). The rewrite rules and the pagination link format mean you can page through one category while the other category page doesn't change.
In functions.php:
function add_new_rules()
{
// new 'paged' variables
global $wp;
$wp->add_query_var('paged_cats');
$wp->add_query_var('paged_dogs');
// rewrite rules
add_rewrite_rule('page/cats/(\d+)/dogs/(\d+)', 'index.php?paged_cats=$matches[1]&paged_dogs=$matches[2]', 'top');
add_rewrite_rule('page/cats/(\d+)/dogs/?$', 'index.php?paged_cats=$matches[1]&paged_dogs=1', 'top');
if( !array_key_exists('page/cats/(\d+)/dogs/(\d+)', (array)get_option('rewrite_rules')) )
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
add_filter('init', 'add_new_rules');
Check for the new query vars in index.php and use them for each WP_Query and the related pagination links.
<div id="blog">
<?php
$paged_cats = (get_query_var('paged_cats')) ? get_query_var('paged_cats') : 1;
$paged_dogs = (get_query_var('paged_dogs')) ? get_query_var('paged_dogs') : 1;
$cats = new WP_query(array(
'category_name' => 'cats',
'paged' => $paged_cats,
'posts_per_page' => 1
));
while( $cats->have_posts() ) : $cats->the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php if ( $cats->max_num_pages > 1 ) : ?>
<div class="navigation">
<?php
echo paginate_links(array(
'base' => home_url("page/cats/%#%/dogs/{$paged_dogs}"),
'format' => '%#%',
'current' => $paged_cats,
'total' => $cats->max_num_pages,
));
?>
</div>
<?php endif; ?>
</div>
<hr>
<div id="blogs">
<?php
$dogs = new WP_query(array(
'category_name' => 'dogs',
'paged' => $paged_dogs,
'posts_per_page' => 1
));
while( $dogs->have_posts() ) : $dogs->the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_postdata();?>
<?php if ( $dogs->max_num_pages > 1 ) : ?>
<div class="navigation">
<?php
echo paginate_links(array(
'base' => home_url("page/cats/{$paged_cats}/dogs/%_%"),
'format' => '%#%',
'current' => $paged_dogs,
'total' => $dogs->max_num_pages,
));
?>
</div>
<?php endif; ?>
</div>
Your problem is this:
On pagination wordpress sends page id or count to get the next enteries and the name of the variable that goes in postback is same for both lists. When it goes to the server both lists get a request to go to next page by looking at the post variables.
The solution mentioned by jho1086 is to create paged variable as a custom variable for both lists and assign it. this would then send a different variable for each list and you can move next or previous as you wish.
You need to do both add a paged variable and add it to your pagination as well. In jho1086's solutuin see $args1 and $pag_args1 both have reference to $paged2 to make this happen.
When you select page 2 for cats it should send a catpage=2 to the server
When you select page 2 for Docs it should send a dogpage=2 to the server
If you can solve this for pagination links and pass the args then you can do following
When server gets the list of cats use catpage as a paging param
When server gets the list of dogs use dogpage as a paging param
This is in theory and will surely work. You can test variables going in and out of the request via firebug and comeback with what other issues you have but the reason your loops go to page 2 for clicking on one is that both loops are paging on the same post variable.
I find answer here https://wordpress.stackexchange.com/questions/47259/multiple-wp-query-loops-with-pagination, the choice answer is working with me. It use format.
<!-- Cats -->
<div class="animals">
<?php
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
// Custom Loop with Pagination 1
// http://codex.wordpress.org/Class_Reference/WP_Query#Usage
$args1 = array(
'paged' => $paged1,
'posts_per_page' => 2,
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post();
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';
endwhile;
// http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $query1->max_num_pages,
'add_args' => array( 'paged2' => $paged2 )
);
echo paginate_links( $pag_args1 );
?>
</div>
<!-- Dogs -->
<div class="animals">
<?php
// Custom Loop with Pagination 2
$args2 = array(
'paged' => $paged2,
'posts_per_page' => 2,
);
$query2 = new WP_Query( $args2 );
while ( $query2->have_posts() ) : $query2->the_post();
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';
endwhile;
$pag_args2 = array(
'format' => '?paged2=%#%',
'current' => $paged2,
'total' => $query2->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args2 );
?>
</div>
And since it is generating a un-clean url you can add a rel="nofollow" for SEO purposes. Here is the instruction how to do add rel="nofollow"
the functions next_post_link() and previous_post_link() has a third argument called 'in_same_cat' - You will need to set it to TRUE.
Read the codex page (click on the functions name in the answer .
From codex :
<?php next_post_link('%link', 'Next post in category', TRUE); ?>
They even have a fourth argument 'excluded_categories' which you can also use for achieving the same thing, or combining both to get even more sophisticated results.

Categories