1

I've created multiple custom post types with archives set to true with a custom slug. However, all of these archives have the body class of blog and home and no class related to the post type.

I'm using <body <?php body_class(); ?>> in the header file that get's called on every page, but somehow it doesn't recognize these pages as cpt-archives.

Is this expected functionality, or is it more likely I've done an error in coding the templates/post types?

Archive example

My post type code (All CPT coded same way, minor variations):

function publikasjoner() { 
    register_post_type( 'publikasjoner',
        array( 'labels' => array(
            'name' => __( 'Publikasjoner', 'bonestheme' ),
            'singular_name' => __( 'Publikasjon', 'bonestheme' ),
            'all_items' => __( 'Alle publikasjoner', 'bonestheme' ),
            'add_new' => __( 'Ny publikasjon', 'bonestheme' ),
            'add_new_item' => __( 'Legg til ny publikasjon', 'bonestheme' ),
            'edit' => __( 'Rediger', 'bonestheme' ),
            'edit_item' => __( 'Rediger publikasjoner', 'bonestheme' ),
            'new_item' => __( 'Ny publikasjon', 'bonestheme' ),
            'view_item' => __( 'Vis publikasjon', 'bonestheme' ),
            'search_items' => __( 'Søk etter publikasjon', 'bonestheme' ),
            'not_found' =>  __( 'Fant ingenting i databasen.', 'bonestheme' ),
            'not_found_in_trash' => __( 'Fant ingenting i søppelkassen', 'bonestheme' ),
            'parent_item_colon' => ''
            ),
            'description' => __( 'Østlandsforsknings publikasjoner legges til her', 'bonestheme' ),
            'public' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'show_ui' => true,
            'query_var' => true,
            'menu_position' => 9,
            'menu_icon' => 'dashicons-edit',
            'rewrite'   => array( 'slug' => 'publikasjoner', 'with_front' => false ),
            'has_archive' => 'publikasjoner',
            'capability_type' => 'post',
            'hierarchical' => true,
            'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'custom-fields', 'revisions')
        )
    );

    register_taxonomy_for_object_type( 'emne', 'publikasjoner' );
    register_taxonomy_for_object_type( 'emneord', 'publikasjoner' );
    register_taxonomy_for_object_type( 'emne', 'type_publikasjon' );

}
  • Is there anything that could be interfering? e.g. anything filtering body_class in the theme or plugins? There should be a lot more than home and blog, are there any caching plugins? – Tom J Nowell Jan 7 '15 at 15:24
  • You should see a lot more classes. Make sure you have the body_class() filter inside the body tag. <body <?php body_class(); ?>> – gdaniel Jan 7 '15 at 15:33
  • @gdaniel I do have the body_class() filter inside the body tag in the header.php file, which get's called on every page. – turtledropbomb Jan 7 '15 at 15:43
  • @TomJNowell is there a an easy way for me to find out, other than disabling plugins? – turtledropbomb Jan 7 '15 at 15:43
  • Try searching your codebase or body_class, your editor should be up to the job, or a tool such as grep – Tom J Nowell Jan 7 '15 at 17:05
3

There's filter called body_class for that.

function my_own_body_classes($classes) {

    // Add Classes to body if the post type archive is 'publikasjoner'
    if ( is_post_type_archive( 'publikasjoner' ) ) {
        $classes[] = 'publikasjoner-archive';
    }
    // Go for other posts types here


    return $classes;
}
add_filter('body_class', 'my_own_body_classes');
0

You said:

I do have the body_class() filter inside the body tag in the header.php file, which get's called on every page.

And the fact that

You should see a lot more classes.

This has the symptoms of a classic mistake (that I've made tons of time) Make sure that the filter on body_class is returning what it should, e.g. this is wrong:

add_filter('body_class', function($classes){
    global $post;
    if ( 'foobar' === $post->post_type ) {
        $classes[] = 'barfoo';
        return $classes;
    }
});

and failing to return classes, which could fubar the process, and should be done as such:

add_filter('body_class', function($classes){
    global $post;
    if ( 'foobar' === $post->post_type ) {
        $classes[] = 'barfoo';
    }
    return $classes;
});

If you don't return on a filter, which would be in the case of my first (wrong) example, it will null out the value.

Not sure if it helps, but I've accidentally done that in the past.

  • I'm not actually using body_class anywhere else but in the body tag, adding your second filter unfortunately does not make any difference either.. – turtledropbomb Jan 8 '15 at 15:29
0

You can pass whatever class or array of classes to the body class function. So if you were to determine that this is indeed the page for 'publikasjoner' then you could add that class to the body class like this:

body_class('publikasjoner');

You can check if you're on an archive page of a post type with is_post_type_archive().

So you can do:

$cpt_class = "not-a-cpt";
if(is_post_type_archive('publikasjoner')){
     $cpt_class = 'publikasjoner';
}
?><html stuff><?php
body_class($cpt_class);

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.