0

WordPress,你有时会激怒我!

在过去的几个小时里,我一直在尝试做的是试图找到一种方法,让我可以使用自定义日期格式(不更改设置中的全局选项)在侧边栏显示存档,这是一项非常复杂的任务,应该真的就像使用 the_date(); 一样简单。例如,我只想制作 2013 年 1 月,而不是 1 月 13 日,同时还有一年在一个列中,另一个在另一列中。

通过调整此代码,我已经成功地完成了自定义日期格式:http: //www.wpbeginner.com/wp-themes/how-to-customize-the-display-of-wordpress-archives-in-your-侧边栏/对此:

function sidebar_archive_list($arclistno = "24") {

    global $wpdb;

        $limit = 0;

        $year_prev = null;

        $months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC");

    foreach($months as $month) :
        $year_current = $month->year;

        if ($year_current != $year_prev){
             if ($year_prev != null){ }
        }
    ?>

    <a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>" title="<?php echo date("F Y", mktime(0, 0, 0, $month->month, 1, $month->year)); ?>"><?php echo date("M y", mktime(0,0,0,$month->month,1,$month->year)); ?></a>

<?php 

    $year_prev = $year_current;

    if(++$limit >= $arclistno) { break; }

    endforeach; 

}

这工作没有问题。

当我尝试将此函数返回的内容拆分为两个单独的列 div 时,问题就来了。我相信我需要将函数变成一个数组,就像你可以从 wp_get_archives('echo=0'); (还有一些额外的代码),但我的 PHP 技能并不高超,而且我无法完全弄清楚如何重新创建相同的东西。我认为它应该看起来像:

array(3) { [0]=> string(86) "January 2013"
           [1]=> string(90) "September 2012"
           [2]=> string(82) "March 2012"
}

当 var_dumped 时。同样值得牢记的是,无论出于何种原因,锚链接也围绕日期,但会回显,而不是打印出来。

我用于内容拆分的代码是:

<?php

    $links = print(sidebar_archive_list());
    $archive_n = count($links);
        for ($i=0;$i<$archive_n;$i++):
            if ($i<$archive_n/2):
                $archive_left = $archive_left.'<li>'.$links[$i].'</li>';
            elseif ($i>=$archive_n/2):
                $archive_right = $archive_right.'<li>'.$links[$i].'</li>';
    endif;
        endfor;

?>                  

<div class="group">
    <ul class="sb_double_column_list alignleft">
        <?php echo $archive_left;?>
 </ul>

 <ul class="sb_double_column_list alignright">
        <?php echo $archive_right;?>
 </ul>
</div>

任何帮助或建议将不胜感激。我对此很着迷!

4

1 回答 1

0

好吧,在搞砸了一点之后,我设法将 wp_get_archives 修改为我自己的自定义函数,这允许我以与尝试单独使用 wp_get_archives 时类似的方式通过列创建代码。

代码如下。

function sidebar_archive_list($args = '') { 

global $wpdb, $wp_locale; 

$defaults = array(
    'limit' => '24',
    'format' => 'html', 'before' => '',
    'after' => '', 'show_post_count' => false,
    'echo' => 0, 'order' => 'DESC',
); 

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


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

$order = strtoupper( $order );
if ( $order !== 'ASC' ) 
    $order = 'DESC'; 


//filters
$where = apply_filters( 'getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'", $r ); 

$join = apply_filters( 'getarchives_join', '', $r );

$output = '';

    $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 $order $limit";

    $key = md5($query);

    $cache = wp_cache_get( 'wp_get_archives' , 'general');

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


    if ( $arcresults ) { 
        $afterafter = $after; 

        foreach ( (array) $arcresults as $arcresult ) { 

            $url = get_month_link( $arcresult->year, $arcresult->month ); 

            $year = date("y", strtotime($arcresult->year));

            $text = sprintf(__('%1$s %2$d'), $wp_locale->get_month_abbrev($wp_locale->get_month($arcresult->month)), $year); 

            if ( $show_post_count ) 
                $after = '&nbsp;('.$arcresult->posts.')' . $afterafter;
            $output .= get_archives_link($url, $text, $format, $before, $after);
        }
    }

if ( $echo ) // If "echo" has been defined as having a value (e.g: 1) then echo $output, if it has no value (0) then return the value.
    echo $output;
else
    return $output;
}

只需将其放入 中functions.php,然后使用sidebar_archive_list(). 请注意,该功能仅支持每月显示,我已将默认值从wp_get_archives. 所以它会返回数组而不是回显它,默认限制是 24(12 个月下降一列,反之亦然)。

于 2013-01-11T21:23:58.797 回答