Registering additional custom body_class

Posted in: Newspaper
Post count: 108

Hi Guys, I’m having trouble adding a custom style class to the body class using the following code.

I’ve tried various versions but can’t seem to get anything to register to the body_class.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++ START CODE SNIPPET +++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

function mk_body_class_stakeholder_articles($classes) {

if (is_singular(‘stakeholder’) && has_term(”, ‘stakeholder_rel_tag’)) {

global $post;
$mk_sh_rel_tag_terms_obj_list = get_the_terms($post->ID, ‘stakeholder_rel_tag’);
if ($mk_sh_rel_tag_terms_obj_list && !is_wp_error($mk_sh_rel_tag_terms_obj_list)) {
$mk_sh_rel_tag_terms_id_string = join(‘,’, wp_list_pluck($mk_sh_rel_tag_terms_obj_list, ‘term_id’));
$mk_sh_articles_args = array(
‘post_type’ => ‘article’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘stakeholder_rel_tag’,
‘field’ => ‘term_id’,
‘terms’ => explode(‘,’, $mk_sh_rel_tag_terms_id_string),
),
),
‘posts_per_page’ => 1,
);

$mk_sh_articles_query = new WP_Query($mk_sh_articles_args);
if ($mk_sh_articles_query->have_posts()) {
$classes[] = ‘mk-stakeholder-article-row-show’;
} else {
$classes[] = ‘mk-stakeholder-article-row-hide’;
}
}

}
return $classes;
}
add_filter(‘body_class’, ‘mk_body_class_stakeholder_articles’);

+++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++++ START CODE SNIPPET +++++++++++++++++++
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

I’m trying to add a new style class to the body if there are related ‘Articles’ with the same ‘Stakeholder Relationship Tag’ (stakeholder_rel_tag). If there are rows it should add the ‘mk-stakeholder-article-row-show’, if there aren’t rows it should add the ‘mk-stakeholder-article-row-hide’ class.

Is there anything about how the tagDiv theme pack that would stop this from working?

Thanks
Matt

Post count: 27744

Hello,

You can set a global class here https://i.imgur.com/UQvZULs.png
But please let me know if those come from a plugin Stakeholder Relationship Tag’ (stakeholder_rel_tag), mk-stakeholder-article-row-show’? Also, what do you want to achieve when you use this code?

Thank you!

Viewing 2 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic.