我们可以设置具有3个月时间限制的Wordpress存档吗?



如何像这样显示存档小部件下拉列表为1月至3月,4月至6月

请使用以下代码获取 3 个月的帖子。

 <?php 
$args = array(
    'date_query' => array(
        array(
            'after'     => 'October 1st, 2016',
            'before'    => array(
                'year'  => 2016,
                'month' => 12,
                'day'   => 31,
            ),
            'inclusive' => true,
        ),
    ),
    'posts_per_page' => -1,
);
$query = new WP_Query( $args ); ?>

您需要根据需要更改月份和年份。

请尝试一下。将此代码放在您的主题函数.php文件中。

<?php
// Creating the widget 
class wpb_widget extends WP_Widget {
    function __construct() {
        parent::__construct(
        // Base ID of your widget
        'wpb_widget',
        // Widget name will appear in UI
        __('WPBeginner Widget', 'wpb_widget_domain'),
        // Widget description
        array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), ) 
        );
    }
    // Creating widget front-end
    public function widget( $args, $instance ) {
        $title = apply_filters( 'widget_title', $instance['title'] );
        $month = $instance['month'];
        $ex=explode("-",$month);
        // before and after widget arguments are defined by themes
        echo $args['before_widget'];
        if ( ! empty( $title ) )
        echo $args['before_title'] . $title . $args['after_title'];
        $date = $ex[1];
        $m=date('m', strtotime($date));
        $after=$ex[0].' 1st, 2017';
        $number = cal_days_in_month(CAL_GREGORIAN, $m, 2017);
        $before=array(
            'year'  => 2017,
            'month' => $m,
            'day'   => $number,
        );
        // This is where you run the code and display the output
        $args = array(
            'post_type' =>"post",
            'date_query' => array(
                array(
                    'after'     => "'".$after."'",
                    'before'    => $before,
                    'inclusive' => true,
                ),
            ),
            'posts_per_page' => -1,
        );
        $querys = new WP_Query( $args );
        if($querys->have_posts()):
            while($querys->have_posts()):
                $querys->the_post();
                echo get_the_title()."<br>";
            endwhile;
        endif;
        echo $args['after_widget'];
    }       
    // Widget Backend 
    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        else {
            $title = __( 'New title', 'wpb_widget_domain' );
        }
        if(isset($instance[ 'month' ])){
            $month=$instance[ 'month' ];
        }else{
            $month='';
        }
        /// Widget admin form ?>
        <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>
        <p>
            <select id="<?php echo $this->get_field_id('month'); ?>" name="<?php echo $this->get_field_name('month'); ?>" class="widefat" style="width:100%;">
                <option value="0">Select Month</option>
                <option <?php selected( $instance['month'], 'january-march'); ?> value="january-march">Jan-Mar</option>
                <option <?php selected( $instance['month'], 'april-june'); ?> value="april-june">April-June</option> 
                <option <?php selected( $instance['month'], 'july-september'); ?> value="july-september">July-Sep.</option>   
                <option <?php selected( $instance['month'], 'october-december'); ?> value="october-december">Oct.-Dec.</option>   
            </select>
        </p>
        <?php 
    }       
    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        $instance['month'] = ( ! empty( $new_instance['month'] ) ) ? strip_tags( $new_instance['month'] ) : '';
        return $instance;
    }
} // Class wpb_widget ends here  ?>

在核心功能下面使用,

<?php wp_get_archives( array( 'type' => 'monthly', 'limit' => 12 ) ); ?>

更多参考 : https://codex.wordpress.org/Function_Reference/wp_get_archives

最新更新