十字星

  • 首页
  • 技术
  • 随笔
  • 瞎折腾
  • 平面设计
  • 文集
  • 留言
  1. 首页
  2. 技术
  3. 正文

WordPress使用Kratos主题,不用插件实现存档模板

2020-08-31 3475点热度 3人点赞 2条评论

以前使用超简洁的WordStar主题,没有关心这个存档页面的问题,最近换了Kratos主题,百度无意间搜到掩耳的站点,参考了掩耳的博客菜单,但是发现文集这项,Kratos主题没有,但又不想使用插件,于是自建了模板页面.

1.如果使用子主题,在子主题文件夹下任意位置创建,否则直接在主题文件夹kratos-master下任意位置创建文件 archives.php ,文件名可根据自己习惯命名,该文件被识别为模板页是由页面顶部注释部分的Template name标示的.

2.文件内容如下

<?php
/**
 * Kratos Cross actions file
 *
 * @category WordPress
 * @package  kratos
 * @author   Cxw (http://ldzch.com/)
 *
 * Template name: 存档页面
 * Description:   A archives page
 */
get_header();
$numberposts = '50';//显示的文章数,可修改
?>
<div class="k-main <?php echo kratos_option('top_select', 'banner'); ?>" style="background:<?php echo kratos_option('g_background', '#f5f5f5'); ?>">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 details">
                <div class="article py-4">
                    <div class="header text-center">
                        <h1 class="title m-0"><?php the_title(); ?></h1>
                    </div>
                
                    <div class="container">
                        <p>此处仅显示最近的<?php echo $numberposts; ?>篇文章</p>
                        <div>
                            <ul>
                                <?php
                                $previous_year = $year = 0;
                                $previous_month = $month = 0;
                                $ul_open = false;
                                
                                $myposts = get_posts('numberposts='.$numberposts.'&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 '</ul></li>';
                                        endif;
                                
                                        echo '<li><p>'; echo $year.'年'.$month.'月'; echo '</p>';
                                        echo '<ul class="archives-list">';
                                        $ul_open = true;
                                    endif;
                                    $previous_year = $year; $previous_month = $month;

                                ?>
                                    <li>
                                        <time><?php the_time('j'); ?>日</time>
                                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a>
                                        <span class="text-muted"><?php comments_number('', '1评论', '%评论'); ?></span>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </div>
                    </div>                
                </div>
            </div>
            <div class="col-lg-4 sidebar d-none d-lg-block">
                <?php dynamic_sidebar('sidebar_tool'); ?>
            </div>
        </div>
    </div>
</div>

<?php get_footer(); ?>

3,登录WordPress后台管理,新建页面,标题填入 文集 ,内容空着不填,页面属性模板选择存档页面,然后点击发布.

4,在菜单中增加页面文集到页头菜单并保存

5,最终成果

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可
标签: PHP WordPress
最后更新:2020-08-31

cxw

技术宅,最喜瞎折腾.

点赞
< 上一篇
下一篇 >
最新 热点 随机
最新 热点 随机
git迁移项目中的某个目录到新项目 winform判断设计模式还是运行时模式 C# 中的where T : class, new() 到底是什么意思? 解决安装.NET失败并提示“无法建立到信任根颁发机构的证书链” 关闭.net4.0的http访问默认代理 删除名称最后带空格的文件夹
Android Studio 多个真实设备无线调试 C#窗口Form大小设置过小时,始终显示为136*39解决办法 北京海淀区发现一块实验田 Hyper-V安装无桌面版CentOS 6.5并配置自启动redis服务之一:创建Hyper-V虚拟机 自定义日历控件迭代 环境VS2019+NET4.6.1 WinAPI函数SetupDiEnumDeviceInterfaces始终返回false解决方案
标签聚合
WinForm Linux IT CentOS Hyper-V Excel PHP W10 WordPress C#
最近评论
alex 发布于 4 年前(01月22日) 博主,请问把代码贴到博客里可以复制是怎么实现的

COPYRIGHT © 2021 十字星. ALL RIGHTS RESERVED

THEME KRATOS MADE BY VTROIS

京ICP备2020032489号-2