D
D
DeniSidorenko2018-05-06 18:58:56
WordPress
DeniSidorenko, 2018-05-06 18:58:56

How to check for template-name and set class for body?

Good afternoon. Made page templates template-name.php and created pages by selecting the desired template. Body and, accordingly, header are loaded from header.php, however, for each body, the layout designer set a specific class that is needed for CSS. Now I need to set the check if( is_name == name-template) { class body = "template-name class"} . (I think I explained clearly). But I don't know how to get the template name into a variable and set the class. Prompt please
For an example I will set a following.
the file is called template-faq.php

<?php
/*
  * Template name: Template FAQ
  * */
?>

At the beginning, such a name
and how to set a check in header.php if this Template loads
. Thanks in advance, I will be very grateful for the answer

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
gwenn, 2018-05-06
@DeniSidorenko

Wordpress has the is_page_template() function. For example, is_page_template('templates/contact.php') if this file is used to generate the page, then the function will return true.
I did something similar, here's an example:

<?php
function custom_body_classes() {
  if ( is_front_page() ) :
    $classes = body_class();
  elseif ( is_page_template('templates/contact.php') ) :
    $classes = body_class('contact_mod');
  elseif ( is_page_template('templates/projects.php') ) :
    $classes = body_class('inner_2_mod');
  else :
    $classes = body_class('inner_mod');
  endif;

  return $classes;
}

Maybe not the most elegant solution, but it works

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question