Wp_parse_args&类别参数

时间:2012-03-02 作者:kevin

我正在尝试使用此代码:

function wp_custom_archive($args = \'\') {
        global $wpdb, $wp_locale;

        $defaults = array(
            \'cat\' => 6, //doing nothing
            \'format\' => \'html\',
            \'show_post_count\' => true,
            \'echo\' => 1
        );

        $r = wp_parse_args( $args, $defaults );
        extract( $r, EXTR_SKIP );

        if ( \'\' != $limit ) {
            $limit = absint($limit);
            $limit = \' LIMIT \'.$limit;
        }

        // over-ride general date format ? 0 = no: use the date format set in Options, 1 = yes: over-ride
        $archive_date_format_over_ride = 0;

        // options for daily archive (only if you over-ride the general date format)
        $archive_day_date_format = \'Y/m/d\';

        // options for weekly archive (only if you over-ride the general date format)
        $archive_week_start_date_format = \'Y/m/d\';
        $archive_week_end_date_format   = \'Y/m/d\';

        if ( !$archive_date_format_over_ride ) {
            $archive_day_date_format = get_option(\'date_format\');
            $archive_week_start_date_format = get_option(\'date_format\');
            $archive_week_end_date_format = get_option(\'date_format\');
        }

        //filters
        $where = apply_filters(\'customarchives_where\', "WHERE post_type = \'post\' AND post_status = \'publish\' AND ", $r );
        $join = apply_filters(\'customarchives_join\', "", $r);

        $output = \'<ul>\';

            $query = "SELECT YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts FROM $wpdb->posts $join $where GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC $limit";

            $key = md5($query);
            $cache = wp_cache_get( \'wp_custom_archive\' , \'general\');
            if ( !isset( $cache[ $key ] ) ) {
                $arcresults = $wpdb->get_results($query);
                $cache[ $key ] = $arcresults;
                wp_cache_set( \'wp_custom_archive\', $cache, \'general\' );
            } else {
                $arcresults = $cache[ $key ];
            }

            /*echo \'<pre>\';
            print_r($arcresults);
            echo \'</pre>\';*/

            if ( $arcresults ) {
                $afterafter = $after;
                foreach ( (array) $arcresults as $arcresult ) {

                    $url = get_month_link( $arcresult->year, $arcresult->month );
                    // translators: 1: month name, 2: 4-digit year 
                    $text = sprintf(__(\'%s\'), $wp_locale->get_month($arcresult->month));
                    $year_text = sprintf(\'<li>%d</li>\', $arcresult->year);
                    if ( $show_post_count )
                        $after = \'&nbsp;(\'.$arcresult->posts.\')\' . $afterafter;
                    $output .= ( $arcresult->year != $temp_year ) ? $year_text : \'\';
                    $output .= get_archives_link($url, $text, $format, $before, $after);

                    $temp_year = $arcresult->year;
                }
            }

        $output .= \'</ul>\';

        if ( $echo )
            echo $output;
        else
            return $output;
    }
发件人this post 使用类别参数按年份和月份显示档案,但迄今为止没有任何运气。

当我更改show_post_count 值到false 它工作得很好,但当我添加一个category参数时,什么都没有发生,如图所示here. 我也尝试过标签和自定义字段,但没有任何效果。

我已经检查了wp_parse_args 函数,它似乎接受正常参数,例如wp_query.

有什么线索吗?

1 个回复
SO网友:Chris_O

更新3/5:

当我最初回答这个问题时,我没有意识到您的代码几乎是wp_get_archives 作用您的代码或原始wp\\u get\\u存档代码中没有支持类别的内容。编写此函数是为了获取日期存档,默认值缺少类型、限制、前后参数。

如果要将类别构建到此函数中,则需要编写自己的where和join过滤器子句。

资料来源:wp-includes/general-template.php

/**
 * Display archive links based on type and format.
 *
 * The \'type\' argument offers a few choices and by default will display monthly
 * archive links. The other options for values are \'daily\', \'weekly\', \'monthly\',
 * \'yearly\', \'postbypost\' or \'alpha\'. Both \'postbypost\' and \'alpha\' display the
 * same archive link list, the difference between the two is that \'alpha\'
 * will order by post title and \'postbypost\' will order by post date.
 *
 * The date archives will logically display dates with links to the archive post
 * page. The \'postbypost\' and \'alpha\' values for \'type\' argument will display
 * the post titles.
 *
 * The \'limit\' argument will only display a limited amount of links, specified
 * by the \'limit\' integer value. By default, there is no limit. The
 * \'show_post_count\' argument will show how many posts are within the archive.
 * By default, the \'show_post_count\' argument is set to false.
 *
 * For the \'format\', \'before\', and \'after\' arguments, see {@link
 * get_archives_link()}. The values of these arguments have to do with that
 * function.
 *
 * @since 1.2.0
 *
 * @param string|array $args Optional. Override defaults.
 */
function wp_get_archives($args = \'\') {
    global $wpdb, $wp_locale;

    $defaults = array(
        \'type\' => \'monthly\', \'limit\' => \'\',
        \'format\' => \'html\', \'before\' => \'\',
        \'after\' => \'\', \'show_post_count\' => false,
        \'echo\' => 1
    );

结束