Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

adjacent posts links WordPress

  • SOLVED

I have a custom post type called "products" with taxonomies.
When I am in products-single.php I want to include a prev/next navigation between adjacent posts from the same category.
[[LINK href="http://cl.ly/3R1v0G252B033P2i341x"]]http://cl.ly/3R1v0G252B033P2i341x[[/LINK]]

I currently have this code but it navigates between posts chronologically:
<div class="products_nav">
<?php next_post_link('%link', '< Previous Product', false); ?> | <?php previous_post_link('%link', 'Next Product >', false); ?>
</div>


P.N
I didn't mentioned:
- yes I'm using the code inside the loop
- the issue is that depending on product it actually "jumps" to a product in wrong category instead of prev/next links.
- if I set true for "in_same_cat" id doesn't shows the navigation any longer.

Answers (5)

2012-01-18

Julio Potier answers:

Hello
Replace "false" by" true"
<div class="products_nav">

<?php next_post_link('%link', '< Previous Product', true); ?> | <?php previous_post_link('%link', 'Next Product >', true); ?>

</div>


I copy you the codex (hidden by SOPA ;p)
<blockquote><strong>Parameters</strong>

<strong>format </strong>
(string) Format string for the link. This is where to control what comes before and after the link. '%link' in string will be replaced with whatever is declared as 'link' (see next parameter). 'Go to %link' will generate "Go to <a href=..." Put HTML tags here to style the final results. Defaults to '%link &raquo;'.
link
(string) Link text to display. Defaults to next post's title ('%title').

<strong>in_same_cat </strong>
(boolean) Indicates whether next post must be within the same category as the current post. If set to TRUE, only posts from the current category will be displayed. Options are:

TRUE
FALSE (Default)

Note that if the post is in both the parent and subcategory, or more than one category, the next post link will lead to the next post in any of those categories.

<strong>excluded_categories </strong>
(string) Numeric category ID(s) from which the next post should not be listed. Separate multiple categories with and; example: '1 and 5 and 15'. There is no default.
In Wordpress 2.2 (only), apparently, concatenating multiple categories for exclusion is done with a comma, not and; example: '1, 5, 15'. Still no default. </blockquote>


Lucian Florian comments:

as I mentioned, if I do that the navigation disappears.


Julio Potier comments:

What is the name of your taxonomy, is it really "<strong>category</strong>" or not ?
If not, you have to add a filter on "<strong>get_previous_post_join</strong>" and "<strong>get_next_post_join</strong>" and modifiy the 2nd param "<strong>$join</strong>" containing
" AND tt.taxonomy = 'category' AND tt.term_id IN (" . implode(',', $cat_array) . ")"
instead of
" AND tt.taxonomy = 'YOUR TAXO' AND tt.term_id IN (" . implode(',', $cat_array) . ")"


Lucian Florian comments:

The Taxonomy name is:
Category Type

Can you post for me the code so I can paste and where?


Julio Potier comments:

Assuming "category-type" is your cat slug (Category Type is a name, don't think this can work), copy this in your <em>functions.php</em> in your theme folder, and let my "true" instead of "false" ;)

function new_prev_next_link( $join )
{
$join = str_replace( "tt.taxonomy = 'category'", "tt.taxonomy = 'category-type'", $join );
return $join;
}
add_filter( 'get_prev_next_join', 'new_prev_next_link', 10, 1 );


Tell me what's going on now.

<em>ps : are you in debug mode ? error_reporting(E_ALL) ?</em>


Lucian Florian comments:

nothing happens.
For some reason it doesn't figures out in which taxonomy is.
I don't have error reporting turned on. How d oI turn error reporting on?


Julio Potier comments:

Just to be sure, can you replace in my code :
return $join;
by
wp_die(var_dump($join));

To enable error reporting, just write "<em>error_reporting(E_ALL);</em>" at the bottom (last line, really) of "<em>wp-config.php</em>"


Julio Potier comments:

and paste me the output of course ^^


Lucian Florian comments:

that gave me an error in config.php.

I solved with the Ambrosite plugin. It did things right.

2012-01-18

Sébastien | French WordpressDesigner answers:

you can use this plugin
[[LINK href="http://wordpress.org/extend/plugins/ambrosite-nextprevious-post-link-plus/"]]http://wordpress.org/extend/plugins/ambrosite-nextprevious-post-link-plus/[[/LINK]]


Lucian Florian comments:

thanks. this one worked well.

2012-01-18

Luis Abarca answers:

Are you using this code inside the loop ?


Luis Abarca comments:

Are you using this code inside the loop ?


Lucian Florian comments:

yes

2012-01-18

She Codes answers:

Try this:
<div class="products_nav">
<?php next_post_link('%link', '< Previous Product', true); ?> | <?php previous_post_link('%link', 'Next Product >', true); ?>
</div>

2012-01-18

John Cotton answers:

This is a very common question...

The conundrum is "what actually constitutes the <em>next</em> post"?

The answer, of course, depends on who's asking. So in your case it's the "next" one in the same category. I assume you mean "next date in the same category"?

Anyway, the answer to this is you have to roll your own query. Although it's possible with query_posts or WP_Query, it's much more straightforward with a SQL query. What you have to do is LIMIT the sql to 2 records (the current one and the "next" one - sorting ASC gives you next, sorting DESC gives you previous).

All you need to do is create the right SQL - in your case, a JOIN with the taxonomy tables to get posts of the same category.



Lucian Florian comments:

and how would I do that? it seems that next_post_link and prev_post_link is very buggy on this specific installation.