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

Get next(prev) post link for custom taxonomy WordPress

  • SOLVED

I tried the function displayed next to the custom posts Taxonomy (product) with the function
next_post_link ('format', 'link', 'in_same_cat', 'excluded_categories');

But now, it returns me a null value. How can this functionality work?

I appreciate your answer, or any given idea.

Answers (6)

2012-08-02

Martin Pham answers:

Hi Robert,
The next_post_link only works on Taxonomy "category".
I can make it work on custom Taxonomy. Please wait, OK?

Regards,
Martin


Robert Truston comments:

Hi Martin,
Well, I'll wait


Robert Truston comments:

you can integrate into my theme? I do not like to use the plugin


Martin Pham comments:

Hi Robert,
I can make it work,
Can you give me information access wp-admin? If you agree please send email: [email protected]

Regards,
Martin


Robert Truston comments:

OK, please check your email


Robert Truston comments:

This question has been solved according to my requirements.

class CTaxNextPrev {
var $taxonomy;
function __construct($taxonomy) {
$this->taxonomy = $taxonomy;
}
function output($show_title = false,$previous = true) {
if ( is_attachment() )
return false;
$post = $this->query($previous);
if ( !$post )
return false;

if($show_title) {
$title = $post->post_title;
}
else {
$title = $previous ? __('Previous Post') : __('Next Post');
}

$rel = $previous ? 'prev' : 'next';

$string = '<a class="navlink" href="'.get_permalink($post).'" rel="'.$rel.'">'.$title.'</a>';

return $string;
}
private function query($previous = true ) {
global $post, $wpdb;

if ( empty( $post ) )
return null;

$current_post_date = $post->post_date;

$join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";

$term = $this->taxonomy;
$cat_array = wp_get_object_terms($post->ID, $term, array('fields' => 'ids'));
$join .= " AND tt.taxonomy = '$term' AND tt.term_id IN (" . implode(',', $cat_array) . ")";

$adjacent = $previous ? 'previous' : 'next';
$op = $previous ? '<' : '>';
$order = $previous ? 'DESC' : 'ASC';


$where = $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' ", $current_post_date, $post->post_type);
$sort = "ORDER BY p.post_date $order LIMIT 1";

$query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
$query_key = 'get_nextprev_post_'.$adjacent . md5($query);
$result = wp_cache_get($query_key, 'counts');
if ( false !== $result )
return $result;

$result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
if ( null === $result )
$result = '';

wp_cache_set($query_key, $result, 'counts');
return $result;
}
}


I put this code in single-product.php

$nextprev = new MarxNextPrev('product');
$next_link = $nextprev->output(true,false);
$prev_link = $nextprev->output(true);
$prev = $prev_link ? '<div class="pag-left">' . $prev_link . '</div>' : '';
$next = $next_link ? '<div class="pag-right">' . $next_link . '</div>' : '';
$nav = '<div class="sep"></div><div class="post-navigation">' . $prev . $next . '</div>';
if ( $prev || $next ) echo $nav;


Robert Truston comments:

sorry, im in-correct
I put this code in single-product.php

$nextprev = new CTaxNextPrev('product');
$next_link = $nextprev->output(true,false);
$prev_link = $nextprev->output(true);
$prev = $prev_link ? '<div class="pag-left">' . $prev_link . '</div>' : '';
$next = $next_link ? '<div class="pag-right">' . $next_link . '</div>' : '';
$nav = '<div class="sep"></div><div class="post-navigation">' . $prev . $next . '</div>';
if ( $prev || $next ) echo $nav;

2012-08-02

Rashad Aliyev answers:

You must use this functions in LOOP. Do you use it in loop?

I mean: http://codex.wordpress.org/The_Loop

2012-08-02

Manoj Raj answers:

You can try using any of the following plugins

http://wordpress.org/extend/plugins/previous-and-next-post-in-same-taxonomy/

http://wordpress.org/extend/plugins/smarter-navigation/

http://wordpress.org/extend/plugins/ambrosite-nextprevious-post-link-plus/


Robert Truston comments:

Thank Manoj Raj, I do not want to use the plugin

2012-08-02

kidrain92 answers:

hi

2012-08-02

Sabby Sam answers:

Hi Try this,

<?php get_header(); ?>
<div id="content">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
echo "paged " . $paged;
$loop = new WP_Query(array('post_type' => 'products',
'post_status' => 'published',
'paged' => $paged,
'posts_per_page' => 8,
'orderby' => 'menuorder',
'caller_get_posts' => 1
));
?>
<div id="wallpaper-container">
<ul class="blog-wallpaper">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php
//$custom = get_post_custom($post->ID);
//$screenshot_url = $custom["screenshot_url"][0];
//$website_url = $custom["website_url"][0];
?>

<?php ob_start();the_post_thumbnail();$toparse=ob_get_contents();ob_end_clean();?>
<? preg_match_all('/src=("[^"]*")/i', $toparse, $img_src); $thumbnail = str_replace("\"", "", $img_src[1][0]);?>

<li id="blog-<?php the_ID(); ?>">
<em><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>

</em>
<a href="<?php the_permalink() ?>">
<img src="<?php bloginfo('stylesheet_directory'); ?>/timthumb.php?src=<?php echo $thumbnail; ?>&w=200&h=150&zc=1" alt="image" />
</a>
</li>
<?php endwhile; ?>
</ul>
</div>

<div class="navigation">
<div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
<div class="alignright"><?php next_posts_link('More &raquo;') ?></div>
</div>

</div><!-- #content -->
<?php get_sidebar();?>
<?php get_footer(); ?>

2012-08-02

Arnav Joy answers:

see this article

http://www.billerickson.net/previous-and-next-post-in-same-taxonomy/