source: front-page-cats/trunk/front_page_cats.php @ 20923

Revision 20923, 1.5 KB checked in by mdawaffe, 5 years ago (diff)

fix for category exclusion in front page cats

Line 
1<?php
2/*
3Plugin Name: Front Page Categories
4Version: 0.3
5Plugin URI: http://wordpress.org/#
6Description: Select categories to display on the front page.
7Author: Ryan Boren
8*/
9
10function fpc_where($where) {
11  // Change this to the categories you want to show on the front page.
12  // Example:  $cats_to_show = '1 2 3 4';
13  $cats_to_show = '1';
14
15        global $wpdb, $wp_query;
16
17        if (! $wp_query->is_home) {
18                return $where;
19        }
20
21        $cat = $cats_to_show;
22        if (stristr($cat,'-')) {
23                // Note: if we have a negative, we ignore all the positives. It must
24                // always mean 'everything /except/ this one'. We should be able to do
25                // multiple negatives but we don't :-(
26                $eq = '!=';
27                $andor = 'AND';
28                $cat = explode('-',$cat);
29                $cat = intval($cat[1]);
30        } else {
31                $eq = '=';
32                $andor = 'OR';
33        }
34
35        $cat_array = explode(' ', $cat);
36        $whichcat .= ' AND (category_id '.$eq.' '.intval($cat_array[0]);
37        $whichcat .= get_category_children($cat_array[0], ' '.$andor.' category_id '.$eq.' ');
38        for ($i = 1; $i < (count($cat_array)); $i = $i + 1) {
39                $whichcat .= ' '.$andor.' category_id '.$eq.' '.intval($cat_array[$i]);
40                $whichcat .= get_category_children($cat_array[$i], ' '.$andor.' category_id '.$eq.' ');
41        }
42        $whichcat .= ')';
43
44        $where .= $whichcat;
45
46  return $where;
47}
48
49function fpc_join($join) {
50        global $wpdb, $wp_query;
51
52        if (! $wp_query->is_home) {
53                return $join;
54        }
55
56        $join .= " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) ";
57
58  return $join;
59}
60
61add_filter('posts_join', 'fpc_join');
62add_filter('posts_where', 'fpc_where');
63
64?>
Note: See TracBrowser for help on using the repository browser.