
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
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!
| Cookie | Duration | Description |
|---|---|---|
| cookielawinfo-checkbox-advertisement | 1 year | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Advertisement". |
| cookielawinfo-checkbox-analytics | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics". |
| cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
| cookielawinfo-checkbox-functional | 11 months | The cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional". |
| cookielawinfo-checkbox-necessary | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary". |
| cookielawinfo-checkbox-others | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other. |
| cookielawinfo-checkbox-performance | 11 months | This cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance". |
| viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
| viewed_cookie_policy | 11 months | The cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data. |
| Cookie | Duration | Description |
|---|---|---|
| _ga | 2 years | This cookie is installed by Google Analytics. The cookie is used to calculate visitor, session, campaign data and keep track of site usage for the site's analytics report. The cookies store information anonymously and assign a randomly generated number to identify unique visitors. |
| _gat_gtag_UA_43963494_1 | 1 minute | This cookie is set by Google and is used to distinguish users. |
| _gid | 1 day | This cookie is installed by Google Analytics. The cookie is used to store information of how visitors use a website and helps in creating an analytics report of how the website is doing. The data collected including the number visitors, the source where they have come from, and the pages visted in an anonymous form. |
| Cookie | Duration | Description |
|---|---|---|
| IDE | 1 year 24 days | Used by Google DoubleClick and stores information about how the user uses the website and any other advertisement before visiting the website. This is used to present users with ads that are relevant to them according to the user profile. |
| test_cookie | 15 minutes | This cookie is set by doubleclick.net. The purpose of the cookie is to determine if the user's browser supports cookies. |
| VISITOR_INFO1_LIVE | 5 months 27 days | This cookie is set by Youtube. Used to track the information of the embedded YouTube videos on a website. |
| YSC | session | This cookies is set by Youtube and is used to track the views of embedded videos. |
| Cookie | Duration | Description |
|---|---|---|
| CONSENT | 16 years 5 months 26 days 13 hours 25 minutes | No description |
| yt-remote-connected-devices | never | This cookies is set by Youtube and stores the user's video player preferences using embedded YouTube video. |
| yt-remote-device-id | never | This cookies is set by Youtube and stores the user's video player preferences using embedded YouTube video. |