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..

выводим список записей wordpress

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:

We transfer the following code to the sidebar-right.php file from the archive.php file:

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:

 

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:

 

Lesson files

zip wordpress theme archive

This lesson ends, next we'll talk about, how to display thumbnail.


Buy WordPress hosting
/* WordPress tutor
Online tutoring services. List of courses I teach
  • Basic web design course;
  • Site layout;
  • General course on CMS WordPress and continuation of the course on template development;
  • Website development in PHP.
Read more on the page WordPress tutor
*/

Nikolaenko Maxim

Director of web studies ProGrafika. I am developing, website design and promotion. Always glad to new blog readers and good clients.


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Templates for WordPress
The best hosting in Ukraine
Stable hosting for Drupal