V
V
Vladimir2016-03-13 15:34:20
PHP
Vladimir, 2016-03-13 15:34:20

How to get body class from wp_termmeta?

Wordpress
There is a table wp_termmeta with cells meta_id, term_id, meta_key, meta_value It is
necessary to display the meta_value value as a css class in the body_class
d4ffeca11aab41b3b98b0ba93fc8734a.png
Google led to this:

// add taxoonomy term to body_class
function lalka_custom_taxonomy_in_body_class( $classes ){
  if( is_singular() )
  {
    $custom_terms = get_term_meta($meta_id, 'custom-class', true );
    if ($custom_terms) {
      foreach ($custom_terms as $custom_term) {
        $classes[] = 'custom-tax-' . $custom_term->slug;
      }
    }
  }
  return $classes;
}

add_filter( 'body_class', 'lalka_custom_taxonomy_in_body_class' );

But it doesn't help

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2016-03-14
@zaharbaz

Issue resolved! In case anyone needs it:

add_filter( 'body_class', 'lalka_custom_taxonomy_in_body_class' );
function lalka_custom_taxonomy_in_body_class( $classes ){
    if (is_tax('product_cat')) {

        $custom_class = !empty( get_queried_object_id() ) ? get_term_meta(get_queried_object_id(), 'custom-class', true) : FALSE;
        if ( !empty($custom_class) ) {
            $classes[] = 'custom-tax-' . $custom_class;
        }

    }
      return $classes;
}

D
Dmitry, 2016-03-13
@dimasmagadan

$custom_terms = get_term_meta($meta_id, 'custom-class', true );
will give you not an array

$custom_terms = get_term_meta($meta_id, 'custom-class', true );
    if (!empty($custom_terms) ) {
        $classes[] = 'custom-tax-' . $custom_terms;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question