0

I would like to add a CSS class to the body if a visitor goes to the second or third etc. page of the website. Or vice-versa: add a class only on the first visited page. The goal is that I can distinguish the first visited page.

Do you have any idea how can I solve this? Javascript? WP_SESSIONS?

0

It seems you are looking for body_class()

You can read more here

A solution for adding a body class to a specific page template:

add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
if ( is_page_template( 'page-example.php' ) ) {
    $classes[] = 'example';
}
return $classes;
}

The result on the front-end:

<body class="page page-id-7 page-template page-template-page-example page-template-page-example-php example">
0

I think you should use cookies in your functions.php file.

<?php
  function customprefix_body_class_filter() {
    $classes[] = 'has-seen-website';
    return $classes;
  }


  $has_seen_website = filter_input( INPUT_COOKIE, 'has_seen_website' );
  if ( !empty( $has_seen_website ) ) {
    add_filter( 'body_class', 'customprefix_body_class_filter' );
  }
  setcookie('has_seen_website', '1', time()+3600*24); // expires in 24 hours 
?>
  • Can I use WP_SESSION also? – Batyuvitez Jun 17 '17 at 12:56
  • Yes you could. The cookie could live longer, depending on the duration you're giving :) – Emilien Schneider Jun 20 '17 at 8:20

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.