Splitting a WordPress Template into Parts - Tutorial 15
At the moment, our theme shows the appearance of the template and now we can proceed to the process of filling and setting up data output.
Create 3 categories (Layout, Design, Website development), put in each category 3 records. If you do not know how to fill in new entries, don't bother, use the service Lorem Ipsum. The service helps to generate arbitrary text. Make text no less than 3 paragraph in each entry.
Analysis template
To display posts, we will use a template with three columns (threecolumn.html). This html file is already loaded and let's change the file extension from html to php. The file needs to be renamed to that name, which wordpress will be able to properly use archive.php.
A file called archive.php is used by wordpress to display a list of articles..
We share the template in the same way as with our file index.php in previous lessons, arrange functions.
<?php get_header(); ?>
<?php get_footer(); ?>
Since we have already done previous lessons, let's not repeat ourselves and move on, this is your own work, then check.
Need to split archive.php template.
To start, let's create 2 file sidebar-left.php and sidebar-right.php. To file sidebar-left.php we transfer the code from the archive.php file which I will show below:
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 |
<section> <header> <h2>Ipsum Dolor</h2> </header> <p> Vehicula fermentum ligula at pretium. Suspendisse semper iaculis eros, eu aliquam iaculis. Phasellus ultrices diam sit amet orci lacinia sed consequat. </p> <ul class="link-list"> <li><a href="#">Sed dolore viverra</a></li> <li><a href="#">Ligula non varius</a></li> <li><a href="#">Dis parturient montes</a></li> <li><a href="#">Nascetur ridiculus</a></li> </ul> </section> <section> <header> <h2>Magna Phasellus</h2> </header> <ul class="link-list"> <li><a href="#">Sed dolore viverra</a></li> <li><a href="#">Ligula non varius</a></li> <li><a href="#">Nec sociis natoque</a></li> <li><a href="#">Penatibus et magnis</a></li> <li><a href="#">Dis parturient montes</a></li> <li><a href="#">Nascetur ridiculus</a></li> </ul> </section> |
We transfer the following code to the sidebar-right.php file from the archive.php file:
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 |
<section> <header> <h2>Magna Phasellus</h2> </header> <ul class="link-list"> <li><a href="#">Sed dolore viverra</a></li> <li><a href="#">Ligula non varius</a></li> <li><a href="#">Nec sociis natoque</a></li> <li><a href="#">Penatibus et magnis</a></li> <li><a href="#">Dis parturient montes</a></li> <li><a href="#">Nascetur ridiculus</a></li> </ul> </section> <section> <header> <h2>Ipsum Dolor</h2> </header> <p> Vehicula fermentum ligula at pretium. Suspendisse semper iaculis eros, eu aliquam iaculis. Phasellus ultrices diam sit amet orci lacinia sed consequat. </p> <ul class="link-list"> <li><a href="#">Sed dolore viverra</a></li> <li><a href="#">Ligula non varius</a></li> <li><a href="#">Dis parturient montes</a></li> <li><a href="#">Nascetur ridiculus</a></li> </ul> </section> |
After creating two files (sidebar-left.php, sidebar-right.php) and transferring code to them, you need to include this code in the archive.php file. The connection is made using the function
<?php get_sidebar(‘left’); ?>. Parentheses indicate the second part of the file name. function get_sidebar(‘left’) includes file sidebar-left.php.
If in your practice in other people's templates you come across a function get_sidebar(‘left’) without attributes in brackets get_sidebar(), it means, that the sidebar was created in the template folder and included with the default name (sidebar.php).
When developing websites, everything must be done gradually and carefully., and after manipulations, the template still turned out to be cumbersome, we need to separate the static parts of the template a little more into separate files.
Let's create two files content-top.php and content-bottom.php. To include these files, we will use the function get_template_part() with two get_template_part attributes( ‘content-top’, get_post_format() ). In the first attribute, we specify the name of the file to be included., and in the second attribute the function get_post_format() which returns the record format. Second attribute optional and that means, that it is simply not required, if you don't use it in the future.. More details about the function can be read in the wordpress code.
Partition index.php template as well as archive.php with get_template_part().
Think, it would be ok not to post long code here, and post the result below and so that you can parse the template yourself. Self-analysis according to the recommendations received will be more effective, than reading a long sheet of information. There will be questions, write in the comments.
Improvements after splitting the template
To correctly display the title and date, use the following code:
1 2 3 4 5 6 7 8 |
<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> |
Let's analyze the template code in order:
the_title_attribute() – displaying the post title for the title attribute.
the_permalink() - displays a link to the entry, until it works, since we haven't set up a single post template yet.
the_title() - used to display the title of the post.
the_time() - displays the date of the current entry. You can use it as in our example or in a slightly different way., examples can be found in official guide.
Also in the archive.php template I used the function the_excerpt() which displays the section of the article before the tag Further, which can be inserted into records. If you need to output not a segment in the archive, and the full article, then you can use the function the_content().
Added functions to the template, useful for styling individual template posts:
post_class() – additional classes useful for theme developers, this function can output the post category, type record. We'll look at it in more detail later., if we need it. If you need to add your own class, then it can be entered as an attribute of the post_class function(‘class-name’).
the_ID() - displays the entry ID in the list of all entries.
Usage example:
1 |
<section class="is-post is-post-excerpt" <?php post_class(); ?> id="post-<?php the_ID(); ?>"> |
Lesson files
This lesson ends, next we'll talk about, how to display thumbnail.
/*
- Basic web design course;
- Site layout;
- General course on CMS WordPress and continuation of the course on template development;
- Website development in PHP.