WordPress.org

Support

Support » How-To and Troubleshooting » Help Me Spot The Error – First Plug-in

Help Me Spot The Error – First Plug-in

  • I’m writing a plugin to sort the categories when they come out of the_category(). I’m basically recycling code from someone else’s plugin for explicit category sorting for sidebars. It works fine and all…except for at least one subcategory: booksmusicmoviestv.

    When that one is present in the post’s category list, it screws up the code:

    Posted in <a href="http://nmallory.exit-23.net/category/the-world/" title="View all posts in The World" rel="category tag">The World</a>, <a href="http://nmallory.exit-23.net/category/the-world/in-the-news/" title="View all posts in In the News" rel="category tag">In the News</a>, <a href="http://nmallory.exit-23.net/category/geekery/" title="View all posts in Geekery" rel="category tag">Geekery</a>, <a href="http://nmallory.exit-23.net/category/geekery/booksmusicmoviestv/" http://nmallory.exit-23.net/category/geekery/interesting-trivia="" title="View all posts in Books, <a href=" rel="category tag">Interesting Trivia</a>, <strong>|</strong> <a href="http://nmallory.exit-23.net/wordpress/wp-admin/post.php?action=edit&amp;post=609">Edit</a><strong>|</strong> <a href="http://nmallory.exit-23.net/20051028/mr-sulus-gay-french-cat#respond" title="Comment on Mr. Sulu's Gay French Cat">No Comments ยป</a>

    See how the link reference is mixed in with whatever’s next? If I put it last, it will just lop onto the next piece of text (“Edit”).

    My plugin code is:
    ‘function get_the_cat_list($separator = ”) {
    $categories = get_the_category();

    if (empty($categories))
    return apply_filters(‘the_category’, __(‘Uncategorized’), $separator, $parents);

    $thelist = ”;

    foreach ( $categories as $category ) {
    $category->cat_name = $category->cat_name;
    $thelist .= $separator . ‘ ‘;
    $thelist .= \’cat_ID) . \'” title=”\’ . sprintf(__(“View all posts in %s”), $category->cat_name) . \'” rel=”category tag”>\’.$category->cat_name.\’\’;

    }

    return $thelist;
    }

    function the_categorysort ($seperator = ”, $partents = ”) {
    global $the_catlist;

    //edit this line with your category slugs
    //inside the quotes, separated by commas.
    $mylist = “featured,the-world,9-11,conspiracy-theories,in-the-news,iraq-afghanistan,natural-disasters,hurricane-katrina,hurricane-rita,politics,soap-box,quote-of-the-day,life,clean-sweep,friends-family,nose-piercing,photos,the-cats,wellness,pet-peeves,geekery,other-blogs,booksmusicmoviestv,computer-gadget-talk,dragoncon,interesting-trivia,roleplaying,web-design,writing,fiction,friday-5-first-sentences,nanowrimo2005,poetry,funnies,discombobulated,misc”;

    //leave the rest alone.
    $the_catlist= “”;
    $rep = “”;

    $thelist = get_the_cat_list(‘,’);

    if (!$mylist) {
    return $thelist; // do nothing if $mylist is blank
    }

    //get rid of multiple tab runs
    //strip list formatting, if any
    $newlist = str_replace(“\t”, $rep, $thelist);
    $postfix = “, “;

    $newlist = explode(“,”, $newlist);

    $sortkeys = explode(‘,’, $mylist);

    $numcatel = count($newlist);
    $sortel = count($sortkeys);

    for ($k = 0; $k < $sortel; $k++) {

    //if unordered list, check for keywords
    //and act on them.

    $searchterm = “/\/(index.php)?(\?)?” . $sortkeys[$k] . “(\/)?\” title=/”;

    for ($i = 0; $i < $numcatel; $i++) {

    if ($catel = preg_match ($searchterm, $newlist[$i])) {
    $the_catlist .= $newlist[$i] . $postfix; //found one
    break;
    }
    }
    }

    return $the_catlist; //we’re done; send it back
    }

    add_action(‘the_category’, ‘the_categorysort’);
    ?>’

    I can’t figure out how it’s getting messed up, but I’m very new to php coding and I don’t know all the ends and outs of the WP code like some folks. Hopefully someone will be able to point me in the right direction.

    Thanks in advance!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Well, I think it has to do with this line:

    $searchterm = "//(index.php)?(?)?" . $sortkeys[$k] . "(/)?\" title=/";

    First of all, I am using permalinks and second, the one I’m having problems with is a subcat.

    Anyone know how to fix this?

    (I’ll continue to blindly try changing things… ๐Ÿ˜‰ )

    Doh! OK I fixed it. The problem actually had to do with the commas (“,”) in the name of the category. I was using commas to delineate the list I was passing around. I’ve switched that to a carrot (^) and it seems to be working now.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Help Me Spot The Error – First Plug-in’ is closed to new replies.