Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter snowcrab302

    (@snowcrab302)

    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;
    }
    }

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

    Thread Starter snowcrab302

    (@snowcrab302)

    Thanks so much! That solved the problem perfectly.

Viewing 2 replies - 1 through 2 (of 2 total)