WordPress免插件仅代码实现文章归档(模板页面)I

时间:2022-04-23
本文章向大家介绍WordPress免插件仅代码实现文章归档(模板页面)I,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

许多博友喜欢为自己的博客建立一个文章归档页面。WordPress 可以用插件来实现,但我们一贯遵守可以不用插件则不用的准则,现在Jeff 就为大家带来免插件仅代码实现文章归档的方法。效果见Jeff的阳台存档页。

关于WordPress 模板是什么自己去谷歌一下吧,注意不要将WordPress 主题跟模板混淆哦!

WordPress 免插件仅代码实现文章归档,coding!

新建一txt文件,打开输入如下代码:

<div class="archives"> <?php $previous_year = $year = 0; $previous_month = $month = 0; $ul_open = false;   $myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC');   foreach($myposts as $post) : setup_postdata($post);   $year = mysql2date('Y', $post->post_date); $month = mysql2date('n', $post->post_date); $day = mysql2date('j', $post->post_date);   if($year != $previous_year || $month != $previous_month) : if($ul_open == true) : echo '</table>'; endif;   echo '<h3>'; echo the_time('F Y'); echo '</h3>'; echo '<table>'; $ul_open = true;   endif;   $previous_year = $year; $previous_month = $month; ?> <tr> <td width="40" style="text-align:right;"><?php the_time('j'); ?>日</td> <td width="400"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></td> <td width="120"><a class="comm" href="<?php comments_link(); ?>" title="查看 <?php the_title(); ?> 的评论"><?php comments_number('0', '1', '%'); ?>人评论</a></td> <td width="120"><span class="view"><?php if(function_exists('the_views')) the_views(); ?>次浏览</span></td> </tr> <?php endforeach; ?> </table> </div>

在主题目录下的style.css中进入以下代码:

.archives td{padding: 6px 10px 8px;border-bottom: solid 1px #eee} .archives table{padding:10px 0 20px} .meta-tit{border-bottom: solid 1px #e6e6e6;padding: 0 0 10px;margin-bottom: 20px}

保存,将txt文件重命名为archives.php,注意不是archives.php.txt哦!然后在wordpress后台,页面-新建页面,题目任取,模板选择“存档”就可以实现了!

注意:

  • 上面代码中的<div id="main">等需要改为符合你的主题的选择器;
  • css样式需要自己修改以符合主题整体样式;
  • 如无特别情况,不建议使用该方法,更好的方法见下面的“建议”;

建议:

就Jeff 实践,该方法有很大的局限性,且是即时输出文章会,一旦文章过多,速度会有影响。不建议使用。更好的方法见下文更新。