当前位置:首页>网站建设>WordPress创建自定义Page模板文件

WordPress创建自定义Page模板文件

释放双眼,带上耳机,听听看~!

默认的WordPress页面文件的模板是page.php,我们也可以根据需要去自定义模版,然后在模版中选择。

在主题目录下新建 template-custom.php(自己取名字) 文件,添加以下代码:

/*
Template Name: 单页面page名称
Template Post Type: page
*/

这个头部很重要,我们只要标识好后在创建page页面的时候右侧页面模板还可以有选择。这样我们只需要选择即可。示范:

<?php
/*
Template Name: 全宽布局模板
Template Post Type: page
*/
get_header(); ?>

<div class="custom-page-container">
    <!-- 自定义页头 -->
    <header class="custom-page-header" style="background: url(<?php echo get_the_post_thumbnail_url(); ?>)">
        <h1><?php the_title(); ?></h1>
        <?php if(get_field('subtitle')): ?>
            <p class="subtitle"><?php the_field('subtitle'); ?></p>
        <?php endif; ?>
    </header>

    <!-- 主内容区 -->
    <main class="custom-page-content">
        <?php 
        while (have_posts()) : the_post();
            the_content();
        endwhile;
        ?>
    </main>

    <!-- 可选侧边栏 -->
    <?php if(is_active_sidebar('custom-page-widget')) : ?>
        <aside class="custom-sidebar">
            <?php dynamic_sidebar('custom-page-widget'); ?>
        </aside>
    <?php endif; ?>
</div>

<?php get_footer(); ?>

如果我们有需要定义的样式也可以单独丢到对应的CSS中。

欢迎访问秀主题博客,分享简单实用WP教程
/* 全宽布局 */
.custom-page-container {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    gap: 30px;
}

.custom-page-header {
    height: 400px;
    background-size: cover !important;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: white;
    text-align: center;
    margin-bottom: 50px;
}

.custom-page-content {
    flex: 3;
    padding: 20px;
}

.custom-sidebar {
    flex: 1;
    background: #f5f5f5;
    padding: 20px;
}

.subtitle {
    font-size: 1.2em;
    opacity: 0.8;
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
网站建设

WordPress使用自定义字段添加关键词和描述信息

2025-4-23 10:21:09

网站建设

WordPress 后台文章列表添加自定义分类法筛选

2025-3-14 14:33:05

温馨提示:

1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:971318156@qq.com,我们将第一时间处理!

2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。

3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索