WP_DROPDOWN_Categories by Date?

时间:2011-04-06 作者:CRIS

有没有办法为wp\\U dropdown\\u categories或wp\\u list\\u categories或其他东西指定一个时间段。。。?我想创建一个归档页面,这样,如果您在2010年的归档中,您将只看到2010年使用的类别。

我必须为此编写自定义函数吗?如果是,有什么建议从哪里开始?

tnx提供任何类型的答案!

顺致敬意,

克里斯

1 个回复
SO网友:Bainternet

我有一个特别的功能,就是为了这个:

<?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;
    }
}
?>
用法:
<?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);
?>

结束