首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我们可以在3个月内设置Wordpress存档吗?

我们可以在3个月内设置Wordpress存档吗?
EN

Stack Overflow用户
提问于 2017-06-13 16:28:24
回答 3查看 148关注 0票数 0

如何将归档小部件下拉列表显示为1-3月、4-6月

EN

回答 3

Stack Overflow用户

发布于 2017-06-13 17:20:56

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

代码语言:javascript
复制
 <?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 ); ?>

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

票数 1
EN

Stack Overflow用户

发布于 2017-06-13 19:28:35

请试一下这个。将此代码放入主题functions.php文件中。

代码语言:javascript
复制
<?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  ?>
票数 1
EN

Stack Overflow用户

发布于 2017-06-13 17:24:20

使用下面的核心功能,

代码语言:javascript
复制
<?php wp_get_archives( array( 'type' => 'monthly', 'limit' => 12 ) ); ?>

有关更多参考,请访问:https://codex.wordpress.org/Function_Reference/wp_get_archives

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44516104

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档