In the first articles of this tutorial we talked about how themes work , how we can create the stylization file and insert a header in this file through which we transmit information about the theme we want to create. We also talked about the template files for the header, footer and sidebar and how to introduce new features to the site using the functions.php file. Next we will present how we customize the other files, respectively the content ones, through which the content of the pages will be introduced and how they work. To enter page content we will use the following scheme:
Use functions for page header and footer templates
In the template files presented we noticed that they contain certain default WordPress functions through which the type of content they provide is specified. Thus, in the header file for example we use the wp_header () function, in the footer file we use the wp_footer () function and so on. We could say that the names of the files are also suggestive and transmit useful information to WordPress about their type. But it is mandatory to introduce these functions as well. We can of course encounter situations in which a developer needs to create more header or footer templates for pages. This will generate different names for each file. But through the functions mentioned above we can more clearly establish their purpose in the content files.
Inserting content files
In content files, WordPress uses two types of methods to customize pages. The first method of content is to add, as appropriate, the necessary templates, and the second is to use a while loop that will search the database assigned to the site for all page-specific posts and display them.
So whether it’s a static page, a post archive page, or just a single post page, we’ll use this scheme by introducing the template invocation feature. for the header. We will introduce the get_header () function at the top of the page and to add the footer at the bottom of the page we put the get_footer () function. In the center of the page we will create a loop for displaying the content and if we want to display a sidebar we will put it before or after the respective loop, depending on its positioning on the page (left or right).
In this loop you can customize the pages by enclosing each default WordPress function for tagged posts, respectively html divs. You will add the styling codes for this tag in the style.css file that we created or in other created CSS files that you will need to invoke in the header as shown in the previous article.
Now we can start customizing the most important content files. The first of these is the index.php file. This must be created because it is the main file that WordPress will use to display the existing content for each page type in the database, when we have not created another specific file to do so. For example, if we do not have a file for the page through which a single post is displayed, respectively single.php, WordPress will generate searches for existing posts through the index.php file, and so on.
The code that we will have to enter in the index.php file is the following:
<?php get_header(); if ( have_posts() ) : while ( have_posts() ) : the_post(); the_content(); endwhile; else : _e( 'Sorry, no posts matched your criteria.', 'textdomain' ); endif; get_sidebar(); get_footer(); ?>
We also mention that the function that invokes the sidebar can be added before or after the loop through which the posts are searched to be displayed.
In the settings in the WordPress control panel we notice that we can choose the type of content that the main page can have. This can be static or blog. If we opt for a static page, WordPress will use the previously created index.php file by default. If we choose the first page to contain an archive of posts, a blog, we will have to create a new page called home.php. The content is similar to that of the post archive pages, respectively arhive.php, which we use to display all blog posts, when they do not appear on the first page. The code to be entered in the home.php and archive.php files is as follows:
<?php get_header(); if ( have_posts() ) : while ( have_posts() ) : the_post(); the_title( '<h2>', '</h2>' ); the_post_thumbnail(); the_excerpt(); endwhile; else: _e( 'Nu au fost gasite rezutate.', 'textdomain' ); endif; get_sidebar(); get_footer(); ?>
Note that the code entered in the posts requires the entry of the post title (using the_title () function), the post cover image (via the_post_thumbnail () function) and its contents (I used the_excerpt () function). To customize your posting pages we can use other default WordPress features that we can add in any order we want it to appear on the page, such as:
next_post_link () – a link to the post published chronologically after the current post
previous_post_link () – a link to the post published chronologically before the current post
the_category () – the category or categories associated with the post or page that is being viewed
() – the main content
the_excerpt () – the first 55 words in the main content of a post, followed by “…”
the_ID () – the post ID
the_meta () – here you can add custom fields created, using their name in parentheses using quotes
the_tags () – post tags
the_title () – post title
the_time () – time or date of post or page.
Next we can create the page for displaying a single post, respectively single.php.
<?php get_header(); if ( have_posts() ) : while ( have_posts() ) : the_post(); the_title( '<h1>', '</h1>' ); the_content(); endwhile; else: _e( 'Nu au fost gasite rezultate.', 'textdomain' ); endif; get_sidebar(); get_footer(); ?>
File integration for templates
In fact, we can create new conditional functions through which we can customize the display of these results according to the page on which they are located. Next we can create custom files following the following principles:
- Creating pages on specific pages – if you want to create a specific page directly from the code without having to go through index.php we will use the phrase filename-pagename.php. For example, if we want to create a page that displays certain custom posts created by us, with the theme “cars”, we will create an archive-car file..php and in it we will add and customize the code as shown above. up.
- Creating tempers. We can use the templates created by assigning them from the WordPress page editing menu, choosing from the “template” section our custom template. To create a template, we will need to create a php file, which we can name as we want, but it should be a suggestive name, for example automobile.php. In this file, before entering the content, we will need to insert a template header as follows:
<?php /** * Template Name: (aici introduci numele template-ului) */ //aici urmeaza sa introduci codul continutului paginii ?>
To continue the tutorial, click here .