0

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.

  • 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 Oct 4 '18 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 Oct 4 '18 at 15:33
0

If 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().

  • This will work for pages if they're using a specific template, but if they are just using the default page.php then the class will apply to all pages using that default template. So, it really depends whether it's just for this one page (and it's using a specific template) or whether OP possibly needs other classes for other cases, such as Categories, Posts, and Tags, which aren't covered by get_page_template_slug(). – WebElaine Oct 4 '18 at 15:34
  • right. I was looking at what he had above which is a template. if there is a need for something besides templates you will have to use conditional logic to figure out what type of page you are on and display the correct class name – RiddleMeThis Oct 4 '18 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.