最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - How to add class dynamically from templates

programmeradmin0浏览0评论

I would like to add dynamic classes from templates to tags like <main>, <header>, <footer> which are defined in header.php and footer.php. This is something I would like to acheive:

header.php

<!doctype html>
<html <?php language_attributes(); ?>>
<head>
    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="profile" href="" />
    <?php wp_head(); ?>
</head>

<body <?php body_class(); ?>>
    <header class="<?php dynamic_header_class(); ?>">
        .
        .
        .
        .
    </header>
    <main class="<?php dynamic_main_class(); ?>">
        <div id="content" class="site-content">

about-template.php

<?php
/*
Template Name: About Page
*/
// Add header-interior class to <header>
set_header_class('header-interior');
// Add main-map-container class to <main>
set_main_class('main-map-container');
?>

contact-template.php

<?php
/*
Template Name: Contact Page
*/
// Add header-exterior class to <header>
set_header_class('header-exterior');
// Add main-no-footer class to <main>
set_main_class('main-no-footer');
?>

Wordpress do have body_class() and post_class() but they are limited to <body> tag and corresponding <div> respectively. Coming from twig which has a very easy way to do these things using {% set %} tag. Can something reusable function or filter be created in wordpress where I could add different classes from different templates to tags like <main>, <header> etc.

发布评论

评论列表(0)

  1. 暂无评论