admin管理员组文章数量:1130349
I just started using wordpress. Now I'm trying to create a theme for a static web page I wrote.
For that web page I'm using barbajs, which is why I have a html tag (in my case a section tag) with a custom data-namespace attribute.
Also I use body classes for each page.
So basically a page looks something like that:
<body class="page-about">
<main>
<section data-namespace="about">
</section>
</main>
</body>
I was able to use a function from this question to append a body class, but since I don't really need all the crap classes that wp comes with (like page page-id-7 page-template, etc), I'd like to create a custom function.
So basically I want something like that:
In the template I'd like to define a namespace variable, or something similar, which will then be used by a function to place wherever I need it.
something like that:
<?php
/* Template Name: About Template */
$namespace = 'about';
?>
<body class="page-<?php get_namespace() ?>">
<main>
<section data-namespace="<?php get_namespace() ?>">
</section>
</main>
</body>
The get_namespace function should then be defined in the functions.php.
Can someone help me with the function, or recommend a better way to do it?
I appreciate all the help!
UPDATE
Now that I've worked on a couple wordpress pages, I understand the way bodyclass operates better. My initial Problem was pretty much only, that I used my own page class which I can replace with the ones created by wordpress in the css, I guess I should simply use static text for the data-namespace attribute, which works fine, as long as I don't include that section in the header.
I just started using wordpress. Now I'm trying to create a theme for a static web page I wrote.
For that web page I'm using barbajs, which is why I have a html tag (in my case a section tag) with a custom data-namespace attribute.
Also I use body classes for each page.
So basically a page looks something like that:
<body class="page-about">
<main>
<section data-namespace="about">
</section>
</main>
</body>
I was able to use a function from this question to append a body class, but since I don't really need all the crap classes that wp comes with (like page page-id-7 page-template, etc), I'd like to create a custom function.
So basically I want something like that:
In the template I'd like to define a namespace variable, or something similar, which will then be used by a function to place wherever I need it.
something like that:
<?php
/* Template Name: About Template */
$namespace = 'about';
?>
<body class="page-<?php get_namespace() ?>">
<main>
<section data-namespace="<?php get_namespace() ?>">
</section>
</main>
</body>
The get_namespace function should then be defined in the functions.php.
Can someone help me with the function, or recommend a better way to do it?
I appreciate all the help!
UPDATE
Now that I've worked on a couple wordpress pages, I understand the way bodyclass operates better. My initial Problem was pretty much only, that I used my own page class which I can replace with the ones created by wordpress in the css, I guess I should simply use static text for the data-namespace attribute, which works fine, as long as I don't include that section in the header.
Share Improve this question edited Oct 31, 2018 at 10:48 josias asked Oct 4, 2018 at 14:10 josiasjosias 1959 bronze badges 2- It sounds like you're kind of trying to reinvent the wheel here. WP may add more classes than you need by default, but it's built that way so plugins and themes can hook on and let you customize things easily. Building additional logic that accomplishes the same thing may cause more problems in the long run than it's worth. – WebElaine Commented Oct 4, 2018 at 15:31
- you are not required to use the body classes that are passed to that function. the answer you linked appends classes, but you can just ignore them all and return whatever you want. – Milo Commented Oct 4, 2018 at 15:33
1 Answer
Reset to default 0If I am understanding you correctly you could use get_page_template_slug(). For example...
<?php echo get_page_template_slug( $post->ID ); ?>
Although, to be honest I don't see the point of doing this over using body_class().
I just started using wordpress. Now I'm trying to create a theme for a static web page I wrote.
For that web page I'm using barbajs, which is why I have a html tag (in my case a section tag) with a custom data-namespace attribute.
Also I use body classes for each page.
So basically a page looks something like that:
<body class="page-about">
<main>
<section data-namespace="about">
</section>
</main>
</body>
I was able to use a function from this question to append a body class, but since I don't really need all the crap classes that wp comes with (like page page-id-7 page-template, etc), I'd like to create a custom function.
So basically I want something like that:
In the template I'd like to define a namespace variable, or something similar, which will then be used by a function to place wherever I need it.
something like that:
<?php
/* Template Name: About Template */
$namespace = 'about';
?>
<body class="page-<?php get_namespace() ?>">
<main>
<section data-namespace="<?php get_namespace() ?>">
</section>
</main>
</body>
The get_namespace function should then be defined in the functions.php.
Can someone help me with the function, or recommend a better way to do it?
I appreciate all the help!
UPDATE
Now that I've worked on a couple wordpress pages, I understand the way bodyclass operates better. My initial Problem was pretty much only, that I used my own page class which I can replace with the ones created by wordpress in the css, I guess I should simply use static text for the data-namespace attribute, which works fine, as long as I don't include that section in the header.
I just started using wordpress. Now I'm trying to create a theme for a static web page I wrote.
For that web page I'm using barbajs, which is why I have a html tag (in my case a section tag) with a custom data-namespace attribute.
Also I use body classes for each page.
So basically a page looks something like that:
<body class="page-about">
<main>
<section data-namespace="about">
</section>
</main>
</body>
I was able to use a function from this question to append a body class, but since I don't really need all the crap classes that wp comes with (like page page-id-7 page-template, etc), I'd like to create a custom function.
So basically I want something like that:
In the template I'd like to define a namespace variable, or something similar, which will then be used by a function to place wherever I need it.
something like that:
<?php
/* Template Name: About Template */
$namespace = 'about';
?>
<body class="page-<?php get_namespace() ?>">
<main>
<section data-namespace="<?php get_namespace() ?>">
</section>
</main>
</body>
The get_namespace function should then be defined in the functions.php.
Can someone help me with the function, or recommend a better way to do it?
I appreciate all the help!
UPDATE
Now that I've worked on a couple wordpress pages, I understand the way bodyclass operates better. My initial Problem was pretty much only, that I used my own page class which I can replace with the ones created by wordpress in the css, I guess I should simply use static text for the data-namespace attribute, which works fine, as long as I don't include that section in the header.
Share Improve this question edited Oct 31, 2018 at 10:48 josias asked Oct 4, 2018 at 14:10 josiasjosias 1959 bronze badges 2- It sounds like you're kind of trying to reinvent the wheel here. WP may add more classes than you need by default, but it's built that way so plugins and themes can hook on and let you customize things easily. Building additional logic that accomplishes the same thing may cause more problems in the long run than it's worth. – WebElaine Commented Oct 4, 2018 at 15:31
- you are not required to use the body classes that are passed to that function. the answer you linked appends classes, but you can just ignore them all and return whatever you want. – Milo Commented Oct 4, 2018 at 15:33
1 Answer
Reset to default 0If I am understanding you correctly you could use get_page_template_slug(). For example...
<?php echo get_page_template_slug( $post->ID ); ?>
Although, to be honest I don't see the point of doing this over using body_class().
本文标签: templatesUpdate body class based on theme as well as a html attribute
版权声明:本文标题:templates - Update body class based on theme as well as a html attribute 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749217531a2334410.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论