Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there any way to specify a time period for wp_dropdown_categories or wp_list_categories or something else...? I want to create a archive page, so that if your in the archive of 2010, you'll see only the categories wich were used in 2010.

do i have to write a custom function for this one? if yes, any suggestion where start?

tnx for any kind of answer!

best regards,

kris

share|improve this question
add comment

1 Answer

I have a fancy function that i made just for that:

<?php
/* Create taxomony term list of posts based on given year 
*
* @param $year - (int) 4 digits - the year to return the list of. required
*
* @param $taxonomy - (string) the taxonomy name of which the retreve the terms. required
* 
* @param $echo_out - (boolean) true - to echo false to return default false.
* 
* @param $type - (array) of args that tels the function what to get.
*
* examples: 
select dropdownlist:
'type' => 'select', //(string)
'name' => 'cat', //(string) name of the select dropdown.
'js' => ture // (boolean) wheter or not to pring out onchange javascript redirect function.

list:
'type' => 'list', //(string)
'before_list' => '<ul>', //(string) html tag (<ul>,<div> you can add classes here).
'after_list' => '</ul>', //(string) html tag (</ul>,</div>)
'before_link' => '<li>', //(string) html tag (<li>,<div>,<span> you can add classes here).
'after_link' => '</li>' //(string) html tag (</li>,</div>,</span>,<br/>).

return array:
'type' => 'return-list', //(string)
'return' => 'ID' // (string) what to return (accepts ID or name).



*/
function yearly_taxonomy_list($year,$type, $taxonomy,$echo_out = false){

    $args = array(
        'year' => $year,
        'posts_per_page' => -1
    );
    $the_query = new WP_Query( $args );
    $cats = array();
    while ($the_query->have_posts()){
        $the_query->the_post();
        $curent_cats = wp_get_object_terms( $post->ID, $taxonomy);
        foreach ($curent_cats as $c){
            if (!in_array($c,$cats)){
                $cats[] = $c;
            }
        }
    }
    //dropdown
    if (isset($type['type']) && $type['type'] =='select'){
        $out = '<select name="'.$type['name'].'" id="'.$type['name'].'" >';
        foreach ($cats as $cd){
            $out .= '<option value='.get_term_link( $cd->term_id, $taxonomy ).'">'.$cd->name.'</option>';
        }
        $out .= '</select>';

        if($type['js']){
            $js = '<script type="text/javascript">
            var dropdown = document.getElementById("'.$name.'");
            function onCatChange() {
                if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
                    location.href = dropdown.options[dropdown.selectedIndex].value;
                }
            }
            dropdown.onchange = onCatChange;
            </script>';
            $out .=  $js;
        }
    }elseif (isset($type['type']) && $type['type'] =='list'){
        $out = $type['before_list'];
        foreach ($cats as $cd){
            $out .= $type['before_link'].'<a href="'.get_term_link( $cd->term_id, $taxonomy ).'">'.$cd->name.'</a>'.$type['after_link'];
        }
        $out .= $type['after_list'];
    }
    //return array
    elseif (isset($type['type']) && $type['type'] =='return-list'){
        //array of  IDS
        if ($type['return'] =='ID'){
            foreach ($cats as $cd){
                $out[] = $cd->term_id;
            }
        }
        //array of  names
        if ($type['return'] =='name'){
            foreach ($cats as $cd){
                $out[] = $cd->name;
            }
        }
        return $out;
    }
    else{
        $out = 'something went worng';
    }
    if($echo_out){
        echo $out;
    }else{
        return $out;
    }
}
?>

Usage:

<?php
// category dropdown with JavaScript OnChange redirect
$type=array('type' => 'select', 'name' => 'cat','js' => ture );
yearly_taxonomy_list(2010,$type, 'category' ,true);

//unordered list of categories
$type=array(
'type' => 'list', 
'before_list' => '<ul class="yearly-cats">', 
'after_list' => '</ul>', 
'before_link' => '<li>',
'after_link' => '</li>');
yearly_taxonomy_list(2010,$type, 'category' ,true);

//return array ids of categories 
$type=array('type' => 'return-list', 'return' => 'ID');
$cat_ids = yearly_taxonomy_list(2010,$type, 'category' ,true);
?>
share|improve this answer
    
Great! Tnx Bainternet! I kinda made my own function already, but i will definitely give yours a try, cos it's looks better that mine, mine is kinda query heavy :) –  CRIS Apr 7 '11 at 0:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.