WordPress自定义页面模板的方法图解

作者:袖梨 2022-06-25

页面模板的作用

WordPress的页面有不同的布局或者样式。wordpress提供了页面功能,可以让我们建立不同的页面以展示不同的内容。如联系方式、留言本等等,很多人都喜欢建个这个。这些页面建立好了,可以自定义标题和内容。但是不同的页面,布局却是完全一样的,没法按自己的需求去改变和添加。有时候我们只想在某一个页面的边栏上添加个什么东西,比如说图片,这时就可以通过自定义模板来实现特定功能的页面。

1、通过ftp工具在你的主题目录下新建一个php文件。比如:links.php(名字随便取)。

2、编辑这个新建的文件,在文件头部加上这段代码。

代码如下 复制代码
/*
Template Name:友链
*/
?>

3、将你的page.php中的内容直接拷贝到links.php当中。

4、然后在links.php 中找到你需要改变的地方。我想,最主要修改的一个是边栏,一个是文章内容,至于怎么改,得看你的需求。

5、修改并保存好这个文件后,创建一个新页面或者修改已存在的页面。在右下边有个“页面模板”的面板,在下拉菜单中选中“友链”后保存就可以了。


links.php当中可以是任何内容,不一定一定要复制page.php中的内容。
甚至你可以在其中直接放上html代码而不加任何其它东西。

创建一个联系(contact)页面模板
如果你的网站有 联系单页面(contact page),这样会很容易让您的网站访问者发送电子邮件到你的WordPress博客管理员。下面是一个示例是新建一个联系人的单页面模板主题,你可以把下面的代码复制下:

代码如下 复制代码


<?php
/*
Template Name: Contact
*/
?>
<?php

$nameError = '';
$emailError = '';
$commentError = '';
$sumError = '';

if(isset($_POST['submitted'])) {
if(trim($_POST['contactName']) === '') {
$nameError = 'Please enter your name.';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}

if(trim($_POST['email']) === '') {
$emailError = 'Please enter your email address.';
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+.[A-Z]{2,4}$", trim($_POST['email']))) {
$emailError = 'You entered an invalid email address.';
$hasError = true;
} else {
$email = trim($_POST['email']);
}

if(trim($_POST['comments']) === '') {
$commentError = 'Please enter a message.';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}

if(trim($_POST['sum']) === '' || trim($_POST['sum']) != '11' ){
$sumError = "Please enter what's 7 + 4";
$hasError = true;
}

if(!isset($hasError)) {
$emailTo = get_option('pu_email');
if (!isset($emailTo) || ($emailTo == '') ){
$emailTo = get_option('admin_email');
}
$subject = '[Contact Form] From '.$name;
$body = "Name: $name nnEmail: $email nnComments: $comments";
$headers = 'From: '.$name.' ' . "rn" . 'Reply-To: ' . $emailTo;

mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}

} ?>

<?php get_header(); ?>




<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

id="post-<?php the_ID(); ?>">

<?php the_title(); ?>







<?php if(isset($emailSent) && $emailSent == true) { ?>


<?php _e('Thanks, your email was sent successfully.', 'framework') ?>




<?php } else { ?>

<?php the_content(); ?>

<?php if(isset($hasError) || isset($captchaError)) { ?>

<?php _e('Sorry, an error occured.', 'framework') ?>


<?php } ?>



  • <?php _e('Name:', 'framework') ?>

    <?php if($nameError != '') { ?>
    <?php echo $nameError; ?>
    <?php } ?>


  • <?php _e('Email:', 'framework') ?>

    <?php if($emailError != '') { ?>
    <?php echo $emailError; ?>
    <?php } ?>


  • textarea"><?php _e('Message:', 'framework') ?>
    <?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?>
    <?php if($commentError != '') { ?>
    <?php echo $commentError; ?>
    <?php } ?>


  • <?php _e('7 + 4:', 'framework') ?>

    <?php if($sumError != '') { ?>

    <?php echo $sumError; ?>
    <?php } ?>




  • <?php _e('Send Email', 'framework') ?>



<?php } ?>





<?php endwhile; else: ?>

>

<?php _e('Error 404 - Not Found', 'framework') ?>




<?php _e("Sorry, but you are looking for something that isn't here.", "framework") ?>


<?php get_search_form(); ?>



<?php endif; ?>




<?php get_sidebar(); ?>

<?php get_footer(); ?>

创建一个宽屏页面模板
全宽页面模板是其主要区别在于,侧边栏已经被删除,使得在内容区域伸展跨越页面宽度的模板。

代码如下 复制代码

<?php
/*
Template Name: Fullwidth
*/
?>

<?php get_header(); ?>





<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

id="post-<?php the_ID(); ?>">

<?php the_title(); ?>




<?php the_content(); ?>
<?php wp_link_pages(array('before' => '

'.__('Pages:', 'framework').' ', 'after' => '

', 'next_or_number' => 'number')); ?>




<?php comments_template('', true); ?>

<?php endwhile; endif; ?>




<?php get_footer(); ?>

创建一个存档页面模板
存档页面模板将显示您的所有旧的文章列表。在这个模板也将按月按类别列出前30天的所有的列表。

代码如下 复制代码

<?php
/*
Template Name: Archives
*/
?>

<?php get_header(); ?>




<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

id="post-<?php the_ID(); ?>">

<?php the_title(); ?>







<?php _e('Last 30 Posts', 'framework') ?>




  • <?php $archive_30 = get_posts('numberposts=30');

foreach($archive_30 as $post) : ?>

<?php endforeach; ?>


<?php _e('Archives by Month:', 'framework') ?>




  • <?php wp_get_archives('type=monthly'); ?>



<?php _e('Archives by Subject:', 'framework') ?>




  • <?php wp_list_categories( 'title_li=' ); ?>







<?php endwhile; else: ?>

>

<?php _e('Error 404 - Not Found', 'framework') ?>




<?php _e("Sorry, but you are looking for something that isn't here.", "framework") ?>


<?php get_search_form(); ?>



<?php endif; ?>



<?php get_sidebar(); ?>

<?php get_footer(); ?>

好了,到这为止,有关创建WordPress的自定义页面模板就介绍到这里。希望你通过这篇文章的简单了解,可以制作出各种漂亮的页面模板。

相关文章

精彩推荐