Need some ninja guru help on a function to exclude cats from loop
-
I’ve been trying to get my admin theme panel gui working so that my users can select from a list of their categories, in order to choose which categories to exclude from the homepage loop.
I believe that I am very close, but can’t seem to get the output correct.
Here is where I am:
I have the following code inside my theme functions file admin panel
// CUSTOM LOOP (homepage)
function category_loopes($options) {
$cats = get_categories('hide_empty=0');
foreach ($cats as $cat) {
$options[] = array( "name" => $cat->cat_name,
"desc" => "Check this box if you wish to exclude this category of articles from the homepage display",
"id" => "zoo_cat_box_".$cat->cat_ID,
"std" => "",
"type" => "checkbox");
}
return $options;
}I then have the ‘selector’ code as:
$options[] = array( "name" => "Homepage Categories to Exclude from homepage articles",
"type" => "heading");
$options = category_loopes($options);Ok so far so good…
What this gives me is another panel in my wp theme admin where it actually lists the categories properly, with a checkbox next to each one.
The user is able to select the categories to exclude and they are saved.
The database has entries in the options table like:
zoo_cat_box_4 true
zoo_cat_box_3 false
Perfect… so i am getting a true in the database for each category to exclude, so far so good.
The problem:
I have to modify the homepage loop to exclude those categories, that part is not working.. Here is the function I am trying for that.
// Custom loops
function get_exclude_categories_loop($labelz) {
$labelz = "zoo_cat_box_";
$include = array();
$counter = 0;
$cats = get_categories('hide_empty=0');
foreach ($cats as $cat) {
$counter++;
if ( get_option( $labelz.$cat->cat_ID ) == 'true') {
$excludecats[] = $cat->cat_ID;
}
}
if(!empty($excludecats)){
$excludecats = implode(',',$excludecats);
}
return $excludecats;
}Finally I place the following on the homepage:
<?php {
$args=array(
'paged'=>$paged,
'category__not_in' => get_option('get_exclude_categories_loop'),
);
};
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();
?>What is happening is that I am seeing all the posts from all the categories on the homepage. It is not excluding the ones I wish it to.
I’m guessing that something is either wrong with my final function, or I am not filtering the loop properly.
Would a code ninja mind taking a peek?
(I know this is not bp related, more wp, but once I learn this, I plan on using the same logic to exclude bp groups from the listings)
sorry for such a long post, I’m really trying hard to learn this stuff the right way….
- The topic ‘Need some ninja guru help on a function to exclude cats from loop’ is closed to new replies.