WordPress templates are PHP files that determine how content is displayed on web pages. Templates can be modified in a theme or overridden in a child theme. The basic template files are header, footer, and other files that make up the overall page. Templates select which content to display, such as individual posts, categories of posts, or search results. Template tags allow themes to display things like post titles, content, dates within templates. The template hierarchy determines which specific template file is used in different contexts.
2. What are WordPress
templates?
Individual PHP files in a theme that
you can modify
Any given page request selects a
particular template
A child theme inherits (and overrides)
the templates of its parent
3. A basic template
<?php
// Includes header.php
get_header();
// Content goes here
// Includes sidebar.php
get_sidebar();
// Includes footer.php
get_footer();
?>
8. What kind of content?
Group of posts
Single post or page
Media attachment
9. Groups of posts
Most recent posts (index.php)
Posts in a specific category
(category.php)
Search results (search.php)
10. The loop
The Loop is used by WordPress to display each
of your posts. Using The Loop, WordPress
processes each of the posts to be displayed on
the current page and formats them according to
how they match specified criteria within The
Loop tags. Any HTML or PHP code placed in
the Loop will be repeated on each post.
WordPress Codex
11. A basic WordPress loop
<?php
while (have_posts()) {
the_post();
// Display post content
}
?>
12. A more sophisticated loop,
with error checking
<?php
if (have_posts()) {
while (have_posts()) {
the_post();
// Display post content
}
} else {
echo "Sorry, no posts were found.n";
}
?>
13. How do you display post
content?
Template Tags are pre-written helper
functions you can use in your theme
For example the_title() which
prints out the current posts title
They are documented extensively
14. Using template tags
<?php
while (have_posts() {
the_post();
?>
<div id="<?php the_ID(); ?>">
<h1><?php the_title(); ?></h1>
<div class="content"><?php the_content(); ?></div>
Posted on <?php the_data(); ?>
at <?php the_time(); ?>
</div>
<?php
}
?>