欢迎访问我们的网站

选择语言

  • English
  • 简体中文
  • 日本語
  • 한국어
  • Français
  • Deutsch
  • Español
  • Português
  • Italiano
  • Русский
  • العربية
  • Nederlands
  • Türkçe
  • Polski
  • Svenska
  • Dansk

WordPress系列

06关于列表详情页面模版的运行机制–新手小白,快速使用静态页面建立wordpress主题

09.09.2025 30

wordpress 列表详情页模板运行机置

1.根据URL确定列表模板:
当访问列表页时,WordPress会根据URL的规则和结构来确定使用哪个模板来显示该列表页。通常情况下,WordPress会按照以下优先级寻找模板:

  • category-{slug}.php:用于特定分类目录的模板,其中{slug}是分类目录的别名(slug)。
  • category-{ID}.php:用于特定分类目录的模板,其中{ID}是分类目录的ID。
  • tag-{slug}.php:用于特定标签页的模板,其中{slug}是标签的别名(slug)。
  • tag-{ID}.php:用于特定标签页的模板,其中{ID}是标签的ID。
  • taxonomy-{taxonomy}-{term}.php:用于特定自定义分类法(taxonomy)和术语(term)的模板。
  • author-{user_nicename}.php:用于特定作者的模板,其中{user_nicename}是作者的用户名。
  • date.php:用于日期归档页的模板。
  • archive.php:用于一般的归档页的模板。
  • index.php:默认的列表页模板。
    2.内容详情模版的调用方式
    文章模版、自定义文章类型内容页模版调用优先级规则
    single-{post-type}-{slug}.php.php
    single-{post-type}.php
    single-{slug}.php
    single.php
    singular.php

4.具体的调用方式
4.1单个模版的调用
$template_path = “template-parts/single-{$category_slug}.php”;//template-parts文件夹的名称,根据自己需求进行设定
4.2多个single.php调用方式—做为参考例子

TEXT
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package cknone
 * @since 1.0.0
 */
//开启定义模版路径开始
// 获取文章所属的第一个分类别名
$categories = get_the_category();
$category_slug = !empty($categories) ? $categories[0]->slug : 'default';
$category_id = !empty($categories) ? $categories[0]->term_id : 0;

// 定义模板路径
$template_path = "template-parts/single-{$category_slug}.php";//template-parts文件夹的名称,根据自己需求进行设定
// echo $template_path;
// 加载模板(若存在)
if (locate_template($template_path)) {
    // 如果当前分类有对应模板,直接使用
    get_template_part('template-parts/single', $category_slug);
} else {
    // 如果当前分类没有模板,检查父级分类
    if ($category_id > 0) {
        $parent_id = get_category($category_id)->parent;
        if ($parent_id > 0) {
            // 获取父级分类信息
            $parent_category = get_term($parent_id, 'category');
            if (!is_wp_error($parent_category) && $parent_category) {
                $parent_slug = $parent_category->slug;
                $parent_template_path = "template-parts/single-{$parent_slug}.php";
                
                // 检查父级分类是否有模板
                if (locate_template($parent_template_path)) {
                    // 使用父级分类的模板
                    get_template_part('template-parts/single', $parent_slug);
                } else {
                    // 如果父级也没有模板,则使用通用模板
                    get_template_part('template-parts/single', 'blogs');
                }
            } else {
                // 如果获取父级分类出错,则使用通用模板
                get_template_part('template-parts/single', 'blogs');
            }
        } else {
            // 如果没有父级分类,则使用通用模板
            get_template_part('template-parts/single', 'blogs');
        }
    } else {
        // 如果分类ID无效,则使用通用模板
        get_template_part('template-parts/single', 'blogs');
    }
}
?>

微信二维码
抖音二维码