Blog / WordPress/ WordPress Template Functions Reference Guide

WordPress Template Functions Reference Guide

WordPress常用模板函数参考文档

Essential Template Files

A standard WordPress theme typically includes the following core template files, which together form the front-end structure of the website.

Filename Description
style.css Theme stylesheet and the file that declares theme information.
index.php Main template file, used as a fallback when other templates are missing.
header.php Header template, usually contains the <head> section and site header.
footer.php Footer template, usually contains footer content and scripts.
sidebar.php Sidebar template.
single.php Used to display a single post.
page.php Used to display a static page.
archive.php Used for archive pages (category, tag, author, date).
category.php Category archive template (higher priority than archive.php).
tag.php Tag archive template (higher priority than archive.php).
search.php Search results page template.
searchform.php Search form template, typically called by get_search_form().
404.php 404 error page template.
comments.php Comment list and comment form template.
front-page.php When a static front page is set, this template takes priority over index.php.

Header Functions

These functions are commonly used in header.php to output site information and resource links.

Function Call Description
<?php bloginfo('name'); ?> Outputs the site title defined in Settings > General.
<?php bloginfo('description'); ?> Outputs the site tagline.
<?php bloginfo('url'); ?> Outputs the site URL.
<?php bloginfo('stylesheet_url'); ?> Outputs the URL of the main theme stylesheet (style.css).
<?php bloginfo('charset'); ?> Outputs the blog charset defined in Settings > Reading.
<?php wp_title(); ?> Outputs the title of the current page. Since WordPress 4.4, wp_get_document_title() is recommended.
<?php wp_head(); ?> Critical function: Must be placed before the </head> tag to allow plugins and themes to add scripts, styles, etc.

Navigation Menus

WordPress provides a powerful menu management system. Here are several common ways to output navigation.

Using the Navigation Menu System

This is the recommended method, allowing users to manage menu items in the Appearance > Menus admin area.

<?php
wp_nav_menu( array(
    'theme_location' => 'primary', // Menu location registered in functions.php
    'menu_class'     => 'main-menu', // CSS class for the menu container
    'fallback_cb'    => 'wp_page_menu', // Fallback function if menu is not set
) );
?>

Category-Based Navigation (Legacy Method)

Directly lists all categories, suitable for simple blog structures.

<ul id='menu'>
    <li <?php if ( is_home() ) echo 'class="current-cat"'; ?>>
        <a href="<?php echo esc_url( home_url( '/' ) ); ?>">Home</a>
    </li>
    <?php wp_list_categories( 'title_li=&orderby=name' ); ?>
</ul>

Page-Based Navigation (Legacy Method)

Directly lists all pages, suitable for simple website structures.

<ul id='menu'>
    <li <?php if ( is_front_page() ) echo 'class="current-page-item"'; ?>>
        <a href="<?php echo esc_url( home_url( '/' ) ); ?>">Home</a>
    </li>
    <?php wp_list_pages( 'sort_column=menu_order&depth=1&title_li=' ); ?>
</ul>

Core Template Functions

These functions are the foundation for building the WordPress loop and content display.

Function Call Description
<?php get_header(); ?> Includes the header.php template file.
<?php get_sidebar(); ?> Includes the sidebar.php template file.
<?php get_footer(); ?> Includes the footer.php template file. Must be called at the end of the template.
<?php if ( have_posts() ) : ?> Checks if the main query has posts.
<?php while ( have_posts() ) : the_post(); ?> Starts the main loop and sets up the current post's data.
<?php the_title(); ?> Outputs the title of the current post.
<?php the_permalink(); ?> Outputs the permanent link (URL) of the current post. Often used with the title: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>.
<?php the_content(); ?> Outputs the full content of the current post (supports pagination).
<?php the_excerpt(); ?> Outputs the excerpt of the current post.
<?php the_time( 'Y-m-d' ); ?> Outputs the post publication time. Parameter is a PHP date format, e.g., 'Y-m-d' outputs '2023-10-27'.
<?php the_category( ', ' ); ?> Outputs the post's categories, separated by a comma.
<?php the_author(); ?> Outputs the post author's name.
<?php comments_template(); ?> Includes the comments.php template to display comments.
<?php endwhile; ?> Ends the while loop.
<?php endif; ?> Ends the if statement.
<?php next_posts_link( 'Older Posts' ); ?> Outputs a link to older posts on archive pages (pagination).
<?php previous_posts_link( 'Newer Posts' ); ?> Outputs a link to newer posts on archive pages (pagination).
<?php edit_post_link( 'Edit' ); ?> Outputs an edit link for the post for users with appropriate permissions.

Main Loop Structure

This is the standard loop structure in WordPress themes for outputting a list of posts.

<?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post(); ?>
        <!-- Output each post's content here -->
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <div class="entry-meta">
                Published on <?php the_time( 'F j, Y' ); ?> | Category: <?php the_category( ', ' ); ?>
            </div>
            <div class="entry-content">
                <?php the_excerpt(); ?>
            </div>
        </article>
    <?php endwhile; ?>
    <!-- Pagination -->
    <div class="pagination">
        <?php next_posts_link( '&laquo; Older Posts' ); ?>
        <?php previous_posts_link( 'Newer Posts &raquo;' ); ?>
    </div>
<?php else : ?>
    <p>Sorry, no content was found.</p>
<?php endif; ?>

Other Useful Functions & Tags

Function/Tag Description
<?php body_class(); ?> Outputs CSS classes for the <body> tag, useful for styling based on page type.
<?php wp_footer(); ?> Critical function: Must be placed before the </body> tag to allow plugins and themes to add footer scripts.
<?php dynamic_sidebar( 'sidebar-1' ); ?> Outputs a registered widget area.
<?php echo esc_url( home_url( '/' ) ); ?> Safely outputs the site's home page link. Recommended over bloginfo('url').
<?php the_post_thumbnail( 'medium' ); ?> Outputs the post's featured image (thumbnail).
<!--more--> Inserted in the post editor to manually define the "Read More" tag truncation point.
<?php _e( 'Text', 'textdomain' ); ?> Translation function, outputs translated text. Used to make themes multilingual.

Post a Comment

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