WordPress / WordPress教程

创建按日期排序伸缩效果的文章归档页

浅时光博客 · 6月14日 · 2021年 31213次已读

一、新建PHP文件


  • 把以下代码保存为一个PHP文件,如:Archives.php,放在主题根目录下
[root@dqzboy wpthems]# vim Archives.php
<?php
function zww_archives_list() {
	if ( !$output = get_option( 'zww_db_cache_archives_list' ) ) {
		$output = '<div id="archives"><h3><a id="al_expand_collapse" href="#" rel="nofollow noopener" >全部展开/收缩</a> <small class="text-muted">(注: 点击月份可以展开)</small></h3>';
		$args = array(
			'post_type' => 'post', //如果你有多个 post type,可以这样 array('post', 'product', 'news') 
			'posts_per_page' => -1, //全部 posts
			'ignore_sticky_posts' => 1 //忽略 sticky posts
		);
		$the_query = new WP_Query( $args );
		$posts_rebuild = array();
		$year = $mon = 0;
		while ( $the_query->have_posts() ): $the_query->the_post();
		$post_year = get_the_time( 'Y' );
		$post_mon = get_the_time( 'm' );
		$post_day = get_the_time( 'd' );
		if ( $year != $post_year )$year = $post_year;
		if ( $mon != $post_mon )$mon = $post_mon;
		$posts_rebuild[ $year ][ $mon ][] = '<li>' . get_the_time( 'd日: ' ) . '<a href="' . get_permalink() . '" rel="nofollow noopener" >' . get_the_title() . '</a> <small class="text-muted">(' . get_comments_number( '0', '1', '%' ) . ')</small></li>';
		endwhile;
		wp_reset_postdata();
		foreach ( $posts_rebuild as $key_y => $y ) {
			$output .= '<h4 class="m-title">' . $key_y . ' 年</h4><ul >'; //输出年份
			foreach ( $y as $key_m => $m ) {
				$posts = '';
				$i = 0;
				foreach ( $m as $p ) {
					++$i;
					$posts .= $p;
				}
				$output .= '<li id="limon"><span class="al_mon">' . $key_m . ' 月 <small class="text-muted"> ( ' . $i . ' 篇文章 )</small></span><ul class="al_post_list">'; //输出月份
				$output .= $posts; //输出 posts
				$output .= '</ul></li>';
			}
			$output .= '</ul>';
		}
		$output .= '</div>';
		update_option( 'zww_db_cache_archives_list', $output );
	}
	echo $output;
}
 
function clear_db_cache_archives_list() {
	update_option( 'zww_db_cache_archives_list', '' ); // 清空 zww_archives_list
}
add_action( 'save_post', 'clear_db_cache_archives_list' ); // 新发表文章/修改文章时
?>

二、引用模板文件


  • 在主题根目录下找到functions.ph原文链接:https://www.dqzboy.comp,引用先前建立的文件
/**
* 引用文章归档函数
**/
include('Archives.php');
#********END*********#

三、创建页面模板


  • 同样在主题根目录下创建一个页面模板文件
[root@dqzboy wpthems]# vim archives_gd.php
<?php
/**
 * Template Name: 归档
 */
get_header(); ?>
<style>
/* ---------------文章归档页面样式--------------------*/
 #main-archivest{margin:20px 0;}
.m-title{text-align:center;border-bottom:solid 1px #ccc;font-size: 18px;font-weight: normal}
.al_mon{font-weight:bold;font-size: 16px;}
#archives h3 a{color:#1E8BC3;font-size: 20px;}
#archives .text-muted{color: #666}
#archives ul li {list-style-type:none;}
#archives ul li a{color: #1E8BC3;font-size: 16px;}
#archives ul >li{padding:0 0 8px 5px;border-left:solid 1px #ccc;background-image:url(https://icss.me/wp-content/uploads/2016/12/li.png); padding-left:20px; background-repeat:no-repeat;}
#archives ul>li>ul{margin:0;padding:0;}
#archives ul>li>ul>li{list-style-type:none;background-image:url(https://icss.me/wp-content/uploads/2016/12/li.jpg);background-position:left 0 top 3px; padding-left:20px;background-repeat:no-repeat; border-left:dashed 1px #ccc;}
</style>
<main class="container">
	<div class="row">
		<div class="md-10 md-offset-1">	
			<div><?php zww_archives_list(); ?></div>
		</div>
	</div>
</main>			
<?php get_footer(); ?>
<script type="text/javascript">
( function ( $, window ) {
	$( function () {
		var $a = $( '#archives' ),
			$m = $( '.al_mon', $a ),
			$l = $( '.al_post_list', $a ),
			$l_f = $( '.al_post_list:first', $a );
		$l . hide();
		$l_f . show();
		$m . css( 'cursor', 's-resize' ) . on( 'click', function () {
			$( this ) . next() . slideToggle( 400 );
		} );
		var animate = function ( index, status, s ) {
			if ( index > $l . length ) {
				return;
			}
			if ( status == 'up' ) {
				$l . eq( index ) . slideUp( s, function () {
					animate( index + 1, status, ( s - 10 < 1 ) ? 0 : s - 10 );
				} );
			} else {
				$l . eq( index ) . slideDown( s, function () {
					animate( index + 1, status, ( s - 10 < 1 ) ? 0 : s - 10 );
				} );
			}
		};
		$( '#al_expand_collapse' ) . on( 'click', function ( e ) {
			e . preventDefault();
			if ( $( this ) . data( 's' ) ) {
				$( this ) . data( 's', '' );
				animate( 0, 'up', 100 );
			} else {
				$( this ) . data( 's', 1 );
				animate( 0, 'down', 100 );
			}
		} );
	} );
} )( jQuery, window );
</script>

四、新建原文链接:https://www.dqzboy.com一个页面


  • 登入WP后台找到页面,然后新建一个页面,模板这里选择你自定义的页面模板文件的变量名称,比如我这里叫做归档原文链接:https://www.dqzboy.com

本文作者:浅时光博客
原文链接:https://www.dqzboy.com/7173.html
版权声明:知识共享署名-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)协议进行许可,转载时请以>超链接形式标明文章原始出处和作者信息
免责声明:本站提供的内容仅限于个人学习和研究使用;禁止将内容用于商业或非法用途。下载后请在24小时内彻底删除,否则后果由用户承担。访问和下载本站内容即表示您已同意上述条款 。

0 条回应

必须 注册 为本站用户, 登录 后才可以发表评论!