Forums

[resolved] next and previous links in one specific category (3 posts)

  1. snowcrab302
    Member
    Posted 5 years ago #

    Hi,

    Is there a way to generate next and previous links for one specific category? (ex. category_name=test).I've tried next_post(), or next_cat_post() but they don't work because some of the posts have more than one category, and wordpress doesn't know which category to choose.

    I've also thought of listing categories to exclude (in the next_post() and next_cat_post), but this would be tedious and messy when I choose to create new categories.

    Can anyone help or link me to some thread I may have missed? Thanks!

  2. snowcrab302
    Member
    Posted 5 years ago #

    I ended up modifying an existing plugin by Scriptygoddess. It seems to work...


    <?php
    /*
    Plugin Name: Next/Previous Post in specific Category
    Version: v 0.1
    Plugin URI:
    Description: This plugin will give you two functions that will give you links to the next/previous post in a specific category.
    Author: hani - based on the plugin by Jennifer - Scriptygoddess
    Author URI:

    */

    /*

    examples of use:
    previous_specCat(13,'', '', '', '', false, '&laquo; previous');
    next_specCat(13,'', '', '', '', false, 'next &raquo;');

    */

    function previous_specCat($specCat,$beforeGroup='<ul>', $afterGroup='</ul>', $beforeEach='<li>', $afterEach='</li>', $showtitle=true, $textForEach='Previous post in %:<br />') {
    global $wpdb, $tableposts, $tablepost2cat, $tablecategories, $post;

    if (!isset($tableposts))
    $tableposts = $wpdb->posts;
    if (!isset($tablepost2cat))
    $tablepost2cat = $wpdb->post2cat;
    if (!isset($tablecategories))
    $tablecategories = $wpdb->categories;

    //get home url for base of link
    $info = get_bloginfo('url');
    $info = apply_filters('bloginfo', $info);
    $homeurl = convert_chars($info);

    //get post id of current post
    $currentPostId = $post->ID;
    //get cat id of current post
    //$currentCatIds = $wpdb->get_results("SELECT category_id FROM $tablepost2cat WHERE post_id = $currentPostId");

    $startGroup = true;

    //get post id of previous post
    $previousPostId = $wpdb->get_var("SELECT post_id from $tablepost2cat, $tableposts WHERE category_id = $specCat AND post_id < $currentPostId AND post_status = 'publish' AND post_id = ID ORDER BY post_id DESC LIMIT 1");

    if ($previousPostId) {
    $previousPostInfo = $wpdb->get_row("SELECT * FROM $tableposts WHERE ID = $previousPostId");

    $previousCatInfo = $wpdb->get_row("SELECT cat_name FROM $tablecategories WHERE cat_ID = $specCat");

    $leaderText = str_replace('%', $previousCatInfo->cat_name, $textForEach);

    if ($startGroup) {
    echo $beforeGroup;
    $startGroup = false;
    }

    if ($showtitle) {
    echo $beforeEach.$leaderText."<a href=\"".get_permalink($previousPostId)."\">".stripslashes($previousPostInfo->post_title)."</a>".$afterEach;
    } else {
    echo $beforeEach."<a href=\"".get_permalink($previousPostId)."\">".$leaderText."</a>".$afterEach;
    }
    }
    }
    if (!$startGroup) {
    echo $afterGroup;
    }

    function next_specCat($specCat, $beforeGroup='<ul>', $afterGroup='</ul>', $beforeEach='<li>', $afterEach='</li>', $showtitle=true, $textForEach='Next post in %:<br />') {
    global $wpdb, $tableposts, $tablepost2cat, $tablecategories, $post;

    if (!isset($tableposts))
    $tableposts = $wpdb->posts;
    if (!isset($tablepost2cat))
    $tablepost2cat = $wpdb->post2cat;
    if (!isset($tablecategories))
    $tablecategories = $wpdb->categories;

    //get home url for base of link
    $info = get_bloginfo('url');
    $info = apply_filters('bloginfo', $info);
    $homeurl = convert_chars($info);

    //get post id of current post
    $currentPostId = $post->ID;

    /* -------------------------------------------------- */
    $nextPostId = $wpdb->get_var("SELECT post_id from $tablepost2cat, $tableposts WHERE category_id = $specCat AND post_id > $currentPostId AND post_status = 'publish' AND post_id = ID ORDER BY post_id ASC LIMIT 1");

    if ($nextPostId) {
    $nextPostInfo = $wpdb->get_row("SELECT * FROM $tableposts WHERE ID = $nextPostId");

    $nextCatInfo = $wpdb->get_row("SELECT cat_name FROM $tablecategories WHERE cat_ID = $specCat");
    $leaderText = str_replace('%', $nextCatInfo->cat_name, $textForEach);

    if ($startGroup) {
    echo $beforeGroup;
    $startGroup = false;
    }

    if ($showtitle) {
    echo $beforeEach.$leaderText."<a href=\"".get_permalink($nextPostId)."\">".stripslashes($nextPostInfo->post_title)."</a>".$afterEach;
    } else {
    echo $beforeEach."<a href=\"".get_permalink($nextPostId)."\">".$leaderText."</a>".$afterEach;
    }
    }

    /* --------------------------------------- */
    }
    ?>

  3. rideforver
    Member
    Posted 5 years ago #

    Well done on the hack! I've been through exactly the same problem, but rather than use a plugin the solution is:

    <?php previous_post_link($format='&laquo %link', $link='%title', $in_same_cat=true, $excluded_categories = 'x,x,x,'); ?>

    For me, using $in_same_cat=true didn't work and I had to exclude all the categories that I didn't want, but for others it has. Hope this helps.

    Further info...

    The Next/Previous Post template tags have now been DEPRECIATED in favour of Next_post_link/Previous_post_link tags.

    You can read all about it on the below thread.

    http://wordpress.org/support/topic/83284

    Thanks to tsguitar for upgrading the codex and finding the solution.

Topic Closed

This topic has been closed to new replies.

About this Topic