You get a bonus - 1 coin for daily activity. Now you have 1 coin

WORDPRESS 24 Sub-Template Files

Lecture



Today we continue the theme of sub-templates, opened in the last lesson with examples of creating files header.php , sidebar.php and footer.php.

Now that the index.php file is divided into zones, everything will be much easier.

Step 1

Before you perform the next operation, look at your sidebar, click on the Archive link. The opened page is no different from your main page, right? Now:

  • create a new file: archive.php
  • copy everything from the index.php file and paste archive.php
  • save archive.php
  • in the archive.php file , replace the_content on the_excerpt
  • save archive.php file again

We created the file archive.php , and made it different from index.php , thus we customized the view of all Archive pages.

Now, when you refresh the archive page, only the summary will be displayed, not the full text of the posts.

Why do you need it? First, to diversify the design and be able to change the appearance of the pages depending on its functions, as well as to prevent punishment from Google and other search engines for duplicating content. If the two pages are exactly the same - this is duplicate content.

What if you have a private blog? Then, it is not necessary to create differences between the archive and main pages. But this does not mean that excerpts are not useful for private blogs.

Also - by default, your category pages will look for instructions on how to display content from the archive.php file . If you do not have archive.php, category pages will be searched in the index.php file .

If you want category pages to look different than the main page and archive pages, create a file category.php and give it the necessary parameters.

Step 2

  • create a new search.php file
  • copy everything from the archive.php file and paste it into search.php
  • save changes

Now all search results will be displayed as a summary. Without the search.php file, the search function will access the index.php file to display the search results.

As needed, to recall the hierarchy of WordPress files, you can go back to lesson number 1.

Step 3

  • create 2 new files: page.php and single.php. Copy everything from the index.php file and paste it into the page.php and single.php files (as long as the page template and the post template should look the same)
  • save all pages

Step 4

I hope you remember that there is a difference between a Page and a page. The name is case sensitive. The page.php template defines the properties of those very special Pages.

First, in the page.php file , enter the following code immediately under ? >:

?
one
'

Страницы: ' , '

' , 'number' ); ?> ); ?>

and

?
one
'Редактировать' , '

' , '

' ); ?> ); ?>

Secondly, remove the postmetadata code from page.php . This is what you should get without postmetadata .

?
one
2
3
four
five
class = "entry" >
'

Страницы: ' , '

' , 'number' ); ?> ); ?>
'Редактировать' , '

' , '

' ); ?> ); ?>

Third, remove the posts_nav_link () or navigation block from page.php .

?
one
2
3
four
five
endwhile ; ?> ; ?>
class = "navigation" >
else : ?>

What happened?

The first line of code was responsible for displaying links to sub-pages.

For example, edit the About Me page. Add the code to the page several times in a paragraph and see what happens. This is useful when you need to break a really long page into several smaller pages.

As for the second line of code, this is the post editing link, which is visible only to the administrator.

Blog pages cannot be included in categories, and since they are static, there is no need to display the date they were created, which is why you deleted postmetadata . Also, it is worth removing posts_nav_link () because it also works only for posts where links to the previous page and the next page are displayed.

Save and close the page.php file .

Step 5

When you click on the post title to see all the material, you go to the view mode of a separate post. The single.php template is responsible for the appearance of the page containing the post.

In file single.php enter the following code under :

?
one
'

Страницы: ' , '

' , 'number' ); ?> ); ?>

Yes, this is all the same line of code for links to sub-pages. Did you know that you can also split the post into several sub-posts?

After that, replace ?> on:


WORDPRESS 24 Sub-Template Files

On the main page, as well as on archive pages, categories and search, you use the posts_nav_link () function to call up links to the previous and next pages. For the page where the post is displayed, there are no such links. Use the previous_post_link () and next_post_link () functions to call up links to the previous and next post.

Save the single.php file. Visit the viewing page of each individual post to see the difference in the navigation area between them.

Overview of the lesson covered

  • We created 4 new files or sub-templates: archive.php, search.php, page.php and sing.php.
  • The archive.php and search.php files are the same.
  • Pages (in other words, non-post items) cannot be combined into categories. Also, they do not have links to the next and previous pages.
  • The s ingle.php template cannot display a link to the number of comments (called by the comments_popup_link () function) and does not use the posts_nav_link () function to call navigation links.

Everything is for today, and very soon we will publish the last lesson of the course “Creating WordPress Themes” and see what to do next :-) In the meantime, ask questions and express your wishes and suggestions in the comments!


Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Content Management Systems (CMS)

Terms: Content Management Systems (CMS)