Skip to:
Content
Pages
Categories
Search
Top
Bottom

Need some ninja guru help on a function to exclude cats from loop

  • @anointed

    Participant

    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….

Viewing 9 replies - 1 through 9 (of 9 total)
  • @avim

    Participant

    Did someone say ninja?

    Sorry, I can’t be more help but maybe this will help kill some time while you wait for some real help!

    @anointed

    Participant

    Well I have narrowed it down to my get_exclude_categories_loop function.

    Here is my new homepage loop code from the codex:

    <?php
    query_posts(array('category__not_in' => array(get_exclude_categories_loop))); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post();
    ?>

    If I replace get_exclude_categories_loop with numbers like 4,6,52 then it works properly.

    This leads me to believe that my get_exclude_categories_loop function is not returning a list of numbers to insert.

    Here is what I do know:

    1. my database has the right information so the UI is working as expected

    zoo_cat_box_4 true

    zoo_cat_box_5 true

    zoo_cat_box_6 false

    2. Using the new query_posts statement above works when on the homepage using hardcoded cat id #s

    3. Leads me to believe that my get_exclude_categories_loop function is not returning numbers

    or

    I can’t call get_exclude_categories_loop in the query_posts like I am trying to do….

    Any ideas what to change to get this working?

    @mrmaz

    Participant

    query_posts() is expecting an array to passed to it for the arg ‘category__not_in’. It looks like you are passing it a string of ids separated by commas. get_exclude_categories_loop() should be returning an array.

    query_posts(array('category__not_in' => array(2,6)));

    See multiple category handling here:

    https://codex.wordpress.org/Template_Tags/query_posts

    This is not valid PHP:

    <?php
    query_posts(array('category__not_in' => array(get_exclude_categories_loop))); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    This is:

    <?php
    query_posts( array( 'category__not_in' => get_exclude_categories_loop() ) ); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    @anointed

    Participant

    @MrMaz

    Thank you for the pointers. Like I said, I am trying hard to learn how this stuff actually works so that I can become more self sufficient.

    I did change out the valid php from above on my homepage, yet it’s still returning all of the posts.

    I think something is wrong with my get_exclude_categories_loop function.

    (It’s a mess as I am not exactly sure what the top half of the function is doing. copied it from another theme, and made so many changes throughout the day that I’ve lost track…. grrr

    I do know that the information is in the database properly. So at least the user UI is working properly :)

    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;

    }

    Is there a way to ‘echo’ out the function so that I can at least see if it’s returning something ‘3,5,7’ etc?

    @anointed

    Participant

    Ok, getting closer:

    <?php
    $excludecatss = get_exclude_categories_loop();
    print_r ($excludecatss);
    ?>

    returns 42,4,46

    That is absolutely correct.

    I’m guessing it must have something to do with:

    <?php
    query_posts( array( 'category__not_in' => get_exclude_categories_loop() ) ); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    I’m so close that I can taste it lol.. man what a day…

    I can’t seem to locate any documentation in the codex about passing a function name to the category__not_in argument, so I’m not sure where to go from there.

    @anointed

    Participant

    I’ve narrowed down the query_posts function to one that works perfectly when I manually type in the numbers 4,42,46 that makes it so those categories are not showing up in the loop.

    This works:

    <?php
    $excludecatss = get_exclude_categories_loop();
    print_r ($excludecatss);
    query_posts(array('category__not_in' => array(4,42,46)));
    ?>

    This does not work:

    When I don’t hardcode in the numbers and try to use my $excludecatss in there instead, it does not filter the loop like the hardcoded one did.

    Tells me this function is not right…

    <?php
    $excludecatss = get_exclude_categories_loop();
    print_r ($excludecatss);
    query_posts(array('category__not_in' => array($excludecatss)));
    ?>

    Both of them print out the proper numbers before running the query, so I know my print_r is returning exactly what I want, 4,42,46

    Where I am stuck is the query_posts part.

    I can’t seem to get that to have $excludecatss put the numbers there so they are excluded….

    @mrmaz

    Participant

    4,42,46 is not an array, that is a string of ids separated by commas. You should not be imploding the ids.

    Get rid of this part of your function:

    if(!empty($excludecats)){
    $excludecats = implode(',',$excludecats);
    }

    You should read up on arrays http://php.net/manual/en/language.types.array.php

    @anointed

    Participant

    Thank you MrMaz

    As it turns out I was being ‘mislead’ by the wp codex, and when you combine that with my limited knowledge of php, I was bound for problems.

    The codex gives an example:

    query_posts(array('category__not_in' => array(2,6)));

    That lead me to believe that I needed to write my function to return a string of id’s separated by commas. Esp when I added in the numbers manually and it worked, that made it really confusing.

    Once I removed those lines and made a small change to the homepage code, it worked perfectly.

    @weblogian

    Participant

    Guys if you want to set exclude category ID from your theme option page. here is a working code.
    ‘category__not_in’ => array(get_option(‘gangmei_blog_cat’)),
    Where gangmei is your theme shortname.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Need some ninja guru help on a function to exclude cats from loop’ is closed to new replies.
Skip to toolbar