SOHO创业

wordpress不同栏目调用不同的头部header模板

在WordPress中,可以使用不同的顶部模板来展示不同栏目的内容。下面是一种方法来实现这个功能:

创建一个新的WordPress主题,或者在现有的主题中创建一个新的目录。
在新目录中创建多个顶部模板文件,例如 top-home.php、top-archive.php、top-category.php、top-search.php 等。
在每个顶部模板文件中,根据需要添加所需的HTML和WordPress代码。这些模板可以根据不同的条件来调用,例如当前页面类型、查询参数等。
在主题的 functions.php 文件中添加以下代码,以便根据不同的栏目调用不同的顶部模板:

function call_top_template() {
    global $wp_query;
    $wp_query->query_vars['template_top'] = 'top-home.php'; // 默认使用 home 栏目的顶部模板

    // 根据当前页面类型判断调用哪个顶部模板
    if (is_archive()) {
        $wp_query->query_vars['template_top'] = 'top-archive.php'; // archive 页面使用 archive 栏目的顶部模板
    } elseif (is_category()) {
        $wp_query->query_vars['template_top'] = 'top-category.php'; // category 页面使用 category 栏目的顶部模板
    } elseif (is_search()) {
        $wp_query->query_vars['template_top'] = 'top-search.php'; // search 页面使用 search 栏目的顶部模板
    }

    // 调用顶部模板
    load_template( locate_template( $wp_query->query_vars['template_top'] ), false, $args );
}
add_action( 'wp', 'call_top_template' );


在主题的 header.php 文件中,将以下代码添加到 标签之前:

<?php call_top_template(); ?>

根据需要,在每个栏目的模板文件中添加所需的HTML和WordPress代码。例如,在 top-home.php 中添加针对主页的顶部样式和内容。
保存文件并上传到WordPress主题目录中。
激活新主题或重新加载现有主题。

现在,根据不同的栏目类型,WordPress将自动调用相应的顶部模板来展示内容。你可以根据需要创建更多的顶部模板文件,并根据条件进行调用。

Published
Categorized as 建站知识 Tagged

By SOHO

wowsoho.com是一个关注跨境电商、创业话题的网站。

wordpress父分类和归档页调用子分类名称和链接

分类名称与分类链接调用问题,category和archives调用下级子分类的名称和链接。

wordpress子分类使用父分类模板

wordpress子分类继承使用父分类模板

The best WordPress service company in China

Jianzhanpress not only excels in WordPress theme design, but also has significant advantages in the field of foreign trade website construction, which can help enterprises achieve success in the international market.

wordpress调用当前页面ID

在wordpress调用当前页面ID代码

wordpress中文官网地址

wordpress中文官网地址是什么

WordPress外贸整站源码的实际应用技巧

     在WordPress外贸建站的过程中,源码的开发与运用是深入了解和定制网站的关键步骤。本文将分享一些… Continue reading WordPress外贸整站源码的实际应用技巧