[wp-hackers] Issue with passing results from get_the_category through do_action -- take 2

Claude Needham gxxaxx at gmail.com
Tue Jul 26 21:46:49 UTC 2011


Sorry about the aborted send of the previous version of this question.
The new version of gmail has a hot key that sends an email. Haven't
figure out what the hot key is yet. :)
So every now and then a partial email gets out the door.

I have written a small bit of code that would allow me to
select the best or most appropriate category for a post. For a post
with just one category this is easy. For a post with multiple
categories this requires a smart routine of some sort.

My problem is in trying to figure out how to properly pass the
$categories object through the do_action system.

The code is something like the following:

in single.php
<?php
$categories = get_the_category();
global $gxxaxx_postcategories;
 $gxxaxx_postcategories = $categories;
foreach($categories as $category) {
    echo $category->slug . '..<br>';
}
?>

in functions.php

function gxxaxx_getbestcategory_handler($categories) {
        global $gxxaxx_postcategories;

        foreach($categories as $category) {
                echo $category->slug . '...<br>';
        }
        foreach($gxxaxx_postcategories as $category) {
                echo $category->slug . '....<br>';
        }
}
add_action('gxxaxx_getbestcategory', 'gxxaxx_getbestcategory_handler', 10, 1);

For a post with a single category the result of this is something like:
design..
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
...
design...

For a post with multiple categories the result is something like:
design..
hosting..
design...
hosting...
design....
hosting....

It looks to be that the do_action is modifying the nature of the
$categories object for a count=1 versus a count>1 array of categories.

Is this normal?

I was trying to avoid the use of globals.

Regards,
Claude Needham
p.s. If this approach is totally out to lunch please advise. However,
I would also like to know how to properly pass values in the
do_action.


More information about the wp-hackers mailing list