wordpress作为日常博客程序,在全世界包括中国都非常受欢迎,它强大的插件和多重的主题功能,让不会代码的站长也可以轻松建站。这篇文章主要介绍在学习wordpress仿站过程中分类页,也就是相当于其他CMS列表页的模板代码。
首先是头部调用,如果不是特殊情况,一般头部就一个文件header.php被调用
<?php get_header();?>
如果是有特殊情况, 也可以是类似这样:
<?php get_header(category);?> //调用header-category.php
接着就是主循环部分了,博客的分类循环体内,一般都有文章标题,文章链接,作者,发布时间,所属栏目,甚至缩略图及摘要等信息。主循环大概这样:
<?php $posts = query_posts($query_string."&orderby=date" ); ?> //主循环查询参数 <?php while(have_posts()) : the_post(); ?> <h3><a href="<?php the_permalink();?>" target="_blank"> <?php the_title();?></a></h3> <ul> <p class="hh"><?php echo get_the_excerpt();?></p> </ul> <p class="dateview"> <span>发布时间:<?php the_time('Y-m-d H:m:s');?></span> <span>作者:<?php the_author();?></span> <span>分类:<?php $category= get_the_category();echo $category[0]->cat_name; ?></span> <a href="<?php the_permalink();?>" target="_blank" class="readmore">阅读全文>></a></p> <div class="line"></div> <?php endwhile; wp_reset_postdata();?>
精简一下是这样:
<?php $posts = query_posts($query_string."&orderby=date" ); ?> //设置主循环查询参数 <?php while(have_posts()) : the_post(); ?> //主循环内的代码 可能被使用的代码如下 <a href="<?php the_permalink();?>" target="_blank"> // 链接 <?php the_title();?> // 标题 <?php echo get_the_excerpt();?> <!-- 摘要 写法1 有[...] <?php // 摘要 写法2 $content = get_the_content(); $trimmed_content = wp_trim_words( $content, 80, '<a href="'. get_permalink() .'"></a>' ); echo $trimmed_content; ?> <?php the_time('Y-m-d H:m:s');?> // 发布时间 <?php the_author();?> //作者 <?php $category= get_the_category();echo $category[0]->cat_name; ?> // 分类 写法1 <?php the_category(',');?> // 分类 写法2 带链接 <?php endwhile; wp_reset_postdata();?> //结束循环,并重置调取的数据 <?php if(function_exists('paging_nav')) paging_nav(); ?> // 调用自已的的分页代码
上面最后是调用了自定义的分页代码,自定义函数都是在主题functions.php里面:
//分页函数 function paging_nav(){ global $wp_query; $big = 999999999; // 需要一个不太可能的整数 $pagination_links = paginate_links( array( 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ), 'format' => '?paged=%#%', 'current' => max( 1, get_query_var('paged') ), 'total' => $wp_query->max_num_pages ) ); echo $pagination_links; }
最后就是调用侧栏和底部了:
<?php get_sidebar();?>或者<?php get_sidebar('category');?> <?php get_footer();?>或者 <?php get_footer('category');?>