如何在WordPress中按“年”和“月”显示“档案”小部件布局(侧边栏)?

时间:2018-04-27 作者:catandmouse

我正在边栏存档中使用默认小部件,当前以以下方式显示存档:

Mar 2018
Feb 2018
Jan 2018
但是,我希望它以这种方式显示:

2018
March
February
January

2017
December
November
October
其中月份是链接。我如何做到这一点?我该如何处理侧边栏。php文件?

2 个回复
最合适的回答,由SO网友:HU ist Sebastian 整理而成

更改默认小部件将非常复杂。

但是,您可以编写自己的短代码和函数来获得所需的列表。

我猜你想在你的小部件中有一个无序的列表?

把这个放在你的主题函数中。php:

add_shortcode(\'archive_by_year_and_month\',\'get_archive_by_year_and_month\');

function get_archive_by_year_and_month($atts=array()){
    global $wpdb;
    $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM $wpdb->posts WHERE post_type = \'post\' AND post_status = \'publish\' ORDER BY post_date DESC");
    if($years){
        $rueckgabe = \'<ul>\';
        foreach($years as $year){
            $rueckgabe.=\'<li class="jahr"><a href="\'.get_year_link($year).\'">\'.$year.\'</a>\';
            $rueckgabe.=\'<ul class="monthlist">\';
            $months = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT MONTH(post_date) FROM $wpdb->posts WHERE post_type=\'post\' AND post_status=\'publish\' AND YEAR(post_date) = %d ORDER BY post_date ASC",$year));
            foreach($months as $month){
                $dateObj   = DateTime::createFromFormat(\'!m\', $month);
                $monthName = $dateObj->format(\'F\'); 
                $rueckgabe.=\'<li class="month"><a href="\'.get_month_link($year,$month).\'">\'.$monthName.\'</a></li>\';
            }
            $rueckgabe.=\'</ul>\';
            $rueckgabe.=\'</li>\';
        }
        $rueckgabe.=\'</ul>\';
    }
    return $rueckgabe;
}
然后将一个文本小部件放入侧栏,并输入短代码:

[archive_by_year_and_month]
点击save,然后看:你应该得到你想要的列表。

快乐的编码!

SO网友:Krzysiek Dróżdż

恐怕,修改原始widget的输出会非常困难。它使用wp_get_archives 函数打印存档文件,没有简单的方法可以修改此输出。你可以尝试使用get_archives_link, 但它可能会变得有点混乱。

说。。。还有其他更简单的方法——编写自己的小部件。

class WP_Widget_ArchivesByYear extends WP_Widget {

    public function __construct() {
        $widget_ops = array(
            \'classname\' => \'widget_archive_by_year\',
            \'description\' => __( \'A monthly archive of your site&#8217;s Posts displayed by year.\' ),
            \'customize_selective_refresh\' => true,
        );
        parent::__construct(\'archives_by_year\', __(\'Archives by Year\'), $widget_ops);
    }

    public function widget( $args, $instance ) {
        global $wpdb;

        $title = apply_filters( \'widget_title\', empty( $instance[\'title\'] ) ? __( \'Archives\' ) : $instance[\'title\'], $instance, $this->id_base );

        echo $args[\'before_widget\'];
        if ( $title ) {
            echo $args[\'before_title\'] . $title . $args[\'after_title\'];
        }

        $years = $wpdb->get_col("SELECT DISTINCT YEAR(post_date) FROM {$wpdb->posts} WHERE post_type = \'post\' AND post_status = \'publish\' ORDER BY post_date DESC");

        if ( $years ) :
        ?>
            <ul class="years-list">
                <?php
                    foreach ( $years as $year ) : 
                        $months = $wpdb->get_col( $wpdb->prepare("SELECT DISTINCT MONTH(post_date) FROM {$wpdb->posts} WHERE post_type=\'post\' AND post_status=\'publish\' AND YEAR(post_date) = %d ORDER BY post_date ASC", $year));
                ?>
                    <li class="year">
                        <a href="<?php echo get_year_link($year); ?>"><?php echo $year ?></a>
                        <ul class="months-list">
                            <?php
                                foreach ( $months as $month ) :
                                    $dateObj = DateTime::createFromFormat(\'!m\', $month);
                            ?>
                                <li class="month">
                                    <a href="<?php echo get_month_link($year, $month); ?>"><?php echo $dateObj->format(\'F\'); ?></a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    </li>
                <?php endforeach; ?>        
            </ul>
        <?php
        endif;

        echo $args[\'after_widget\'];
    }

    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $new_instance = wp_parse_args( (array) $new_instance, array( \'title\' => \'\') );
        $instance[\'title\'] = sanitize_text_field( $new_instance[\'title\'] );

        return $instance;
    }


    public function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( \'title\' => \'\') );
        $title = sanitize_text_field( $instance[\'title\'] );
        ?>
        <p><label for="<?php echo $this->get_field_id(\'title\'); ?>"><?php _e(\'Title:\'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id(\'title\'); ?>" name="<?php echo $this->get_field_name(\'title\'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
        <?php
    }
}
另外,我还没有测试那个代码,所以它可能包含一些拼写错误。

结束

相关推荐

widgets not working

我粘贴了我的siderbar小部件代码widgets.php 文件 function right_sidebar_widget() { register_sidebar( array( \'name\' => \'Right Siderbar\', \'id\' => \'right-siderbar\', \'description\' => \'Right Sidebar\',