Frank Miles

WordPress & Web Developer

  • Home
  • Services
  • Projects

Add body class to single posts

Add custom body class to post by category
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//adds new body class for post category
add_filter('body_class', 'add_category_class_single');
function add_category_class_single($classes){
global $post;
if(is_single()) {
    $category = get_the_category($post->ID);
    $slug = $category[23]->slug;
    $classes[] = 'post-category-video' . $slug;
    }
    return $classes;
}

Genesis base functions.php

functions.php
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
//* Start the engine
include_once( get_template_directory() . '/lib/init.php' );
include_once( get_stylesheet_directory() . '/lib/theme-custom-functions.php' );
//* Child theme (do not remove)
define( 'CHILD_THEME_NAME', 'Genesis Sample Theme' );
define( 'CHILD_THEME_URL', 'http://www.studiopress.com/' );
define( 'CHILD_THEME_VERSION', '2.1.2' );
 
//* Enqueue Styles and Scripts
add_action( 'wp_enqueue_scripts', 'genesis_sample_scripts' );
function genesis_sample_scripts() {
 
//* Add Google Fonts
wp_register_style( 'lato', '//fonts.googleapis.com/css?family=Lato:300,400,700,900', array(), CHILD_THEME_VERSION );
wp_enqueue_style( 'lato' );
wp_register_style( 'montserrat', '//fonts.googleapis.com/css?family=Montserrat:400,700', array(), CHILD_THEME_VERSION );
wp_enqueue_style( 'montserrat' );
wp_register_style( 'lora', '//fonts.googleapis.com/css?family=Libre+Baskerville:400,400italic,700,700italic', array(), CHILD_THEME_VERSION );
wp_enqueue_style( 'lora' );
//* Remove default CSS
wp_dequeue_style( 'genesis-sample-theme' );
 
//* Add compiled CSS
wp_register_style( 'genesis-sample-styles', get_stylesheet_directory_uri() . '/style.css', array(), CHILD_THEME_VERSION );
wp_enqueue_style( 'genesis-sample-styles' );
 
//* Add compiled JS
wp_enqueue_script( 'genesis-sample-scripts', get_stylesheet_directory_uri() . '/js/project' . $minnified . '.js', array( 'jquery' ), CHILD_THEME_VERSION, true );
 
//* Retina images
wp_enqueue_script( 'retina-images', get_stylesheet_directory_uri() . '/js/retina.min.js', array( 'jquery' ), CHILD_THEME_VERSION, true );
}
 
//* Add HTML5 markup structure
add_theme_support( 'html5', array( 'search-form', 'comment-form', 'comment-list' ) );
 
//* Add viewport meta tag for mobile browsers
add_theme_support( 'genesis-responsive-viewport' );
 
//* Add support for custom background
add_theme_support( 'custom-background' );
 
//* Add support for full width footer widget
add_theme_support( 'genesis-footer-widgets', 2 );

theme-custom-functions.php
PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
 
//* Enqueue Styles and Scripts
add_action( 'wp_enqueue_scripts', 'genesis_sample_scripts' );
function genesis_sample_scripts() {
 
  //* Add Google Fonts
  wp_register_style( 'merriweather', '//fonts.googleapis.com/css?family=Merriweather:300,300italic', array(), CHILD_THEME_VERSION );
  wp_enqueue_style( 'merriweather' );
 
  wp_register_style( 'lato', '//fonts.googleapis.com/css?family=Lato:300,400,700,900', array(), CHILD_THEME_VERSION );
  wp_enqueue_style( 'lato' );
 
  wp_register_style( 'montserrat', '//fonts.googleapis.com/css?family=Montserrat:400,700', array(), CHILD_THEME_VERSION );
  wp_enqueue_style( 'montserrat' );
 
  //* Remove default CSS
  wp_dequeue_style( 'genesis-sample-theme' );
 
  //* Add compiled CSS
  wp_register_style( 'genesis-sample-styles', get_stylesheet_directory_uri() . '/style.css', array(), CHILD_THEME_VERSION );
  wp_enqueue_style( 'genesis-sample-styles' );
 
  //* Add compiled JS
  wp_enqueue_script( 'genesis-sample-scripts', get_stylesheet_directory_uri() . '/js/app.js', array( 'jquery' ), CHILD_THEME_VERSION, true );
 
  wp_enqueue_style( 'dashicons' );
 
 
}
 
// Customize site header
remove_action('genesis_header', 'genesis_do_header');
remove_action('genesis_header', 'genesis_header_markup_open', 5);
remove_action('genesis_header', 'genesis_header_markup_close', 15);
function custom_header() {
  ?>
  <header class="site-header">
    <div class="wrap">
    <h1 class="site-title">
       <a href="<?=get_bloginfo('home'); ?>" alt="<?php print get_bloginfo('name') .' - '. get_bloginfo('description'); ?>">
       Woocommerce
       </a>
       </h1>
       <h2 class="site-description">Development theme<!-- <span class="dashicons dashicons-menu"></span> --></h2>
 
    </div>  
  </header>
  <?php }
 
add_action('genesis_header', 'custom_header');
 
 
//* Customize the entire footer
remove_action( 'genesis_footer', 'genesis_do_footer' );
add_action( 'genesis_footer', 'sp_custom_footer' );
function sp_custom_footer() {
?>
<p>&copy; 2015. All Rights Reserved. <a href="contact-me">Contact me</a></p>
<?php
}
 
// add category nicenames in body and post class
add_filter( 'body_class', 'gk_add_body_class' );
function gk_add_body_class( $classes ) {
   $classes[] = 'custom-class';
   if ( !empty( $_GET['bg'] ) )
      $classes[] = 'bg' . esc_attr( $_GET['bg'] );
  
   return $classes;
}

Contact me

Thanks for taking the time to check out my work, and for contacting me. Please fill out the form information and tell me a little about your project, and I'll reply to you within 24 hours.

I look forward to speaking with you.

© 2015. All Rights Reserved