Customizing Page Templates in WordPress – lesson 23
Page templates in WordPress are very easy to add., if you need to style the template for all pages, then you can create a file page.php in the root of the theme. Let's create a file and put the code from the single.php file there. Template created, then you can remove and add something of your own (new design elements, additional sidebars, menu …).
That, what happened can be seen on the example of any previously created page, i clicked on the portfolio page and this is what it looks like now.
We'll talk about the portfolio page later., and now let's try to make templates for individual pages, those that we can then select in the admin panel.
If we need to make a template for a separate page, then you can use a special comment. This comment is a tag for wordpress, which indicates, that the file is an arbitrary pattern.
1 2 3 4 5 |
<?php /* Template Name: example */ ?> |
Let's try to start creating our page templates in order.
At the root of your theme, there is a file called onecolumn.html, rename it to tmp-onecolumn.php, prefix tmp we add, so that in the future you won’t get confused where the page templates are, where are the other files?. After renaming the file, add a comment like this to the top of the template:
1 2 3 4 5 |
<?php /* Template Name: One column */ ?> |
Let's go through the steps already familiar from the previous lessons and replace the upper and lower parts of the code, include a header in the template file, footer, top and bottom of template.
In order for the template to start working, we must specify for which page it needs to be applied.. Go to the admin panel -> Pages -> about and select a new template and refresh the page.
You can see, what the page looks like in its current form but it does not display data yet, only outputs the contents of the tmp-onecolumn.php template. Let's point out together, what and where we will display in the template.
Replacing the content of the tag <section> to the code that we have already used in the page.php template, namely this one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!-- Main Content --> <?php if ( have_posts() ) { while ( have_posts() ) : the_post(); ?> <section class="is-post is-post-excerpt" <?php post_class(); ?> id="post-<?php the_ID(); ?>"> <header> <h2 class="entry-title"><a title="<?php echo the_title_attribute( 'echo=0' ); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2><br /> <span class="date"> <span class="month"><?php the_time('M'); ?></span> <span class="day"><?php the_time('j'); ?></span> <span class="year"><?php the_time('Y'); ?></span> </span> </header> <div class="thumb"> <a href="<?php the_permalink(); ?>" class="image image-full"> <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?> </a> </div> <?php the_content(); ?> <br clear="all"> </section> <div id="single-nav" class="clearfix"> <div class="prev"><?php next_post_link('%link', '<span>%title</span>', false); ?></div> <div class="next"><?php previous_post_link('%link', '<span>%title</span>', false); ?></div> <div class="prev-responsive"><?php next_post_link('%link', '<span>Previous</span>', false); ?></div> <div class="next-responsive"><?php previous_post_link('%link', '<span>Next</span>', false); ?></div> </div> <!-- /single-nav --> <?php endwhile; ?> <?php } ?> |
Further, if we look at the output of the material, we'll see, that the text on the page is shifted to the right, although we haven't inserted the thumbnail yet, this point needs to be fixed everywhere where we used the thumbnail output.
Thumbnail of my video codec output
Let's change this code:
1 2 3 4 5 6 7 |
<div class="thumb"> <a href="<?php the_permalink(); ?>" class="image image-full"> <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?> </a> </div> |
On this one:
1 2 3 4 5 6 7 |
<?php if ( has_post_thumbnail() ) { ?> <div class="thumb"> <a href="<?php the_permalink(); ?>" class="image image-full"> <?php the_post_thumbnail(); ?> </a> </div> <? } ?> |
Before code change.
After code change.
I think, from the images above it is clear, what fix were you talking about?. Also fix the thumbnail output in archive.php files, single.php and page.php alone.
Good luck in website development, keep in touch, I'll tell you a lot more. In the next lesson I will tell, how to display a blog section on a separate page.
Sources of the current lesson
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.