P
P
partyzanx2018-06-10 14:56:08
WordPress
partyzanx, 2018-06-10 14:56:08

How to add class to ARTICLE but only in categories in wordpress?

There is this line:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

She gives birth to such a result
<article id="post-18" class="post-18 post type-post status-publish format-standard hentry category-smart_chinese category-level_1">

How to add itisentry to class on category page
class="itisentry post-18 post type-post status-publish format-standard hentry category-smart_chinese category-level_1"

Usually they do it like this. But this is just adding a class, but it is necessary that the class is added not on the article page, but in categories . Something like that: Usually it is done like this<?php post_class( 'class1 class2 class3' ); ?>
<?php post_class( 'if (is_category()) { echo "itisentry"; }' ); ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yanchevsky, 2018-06-10
@partyzanx

So?

function category_id_class( $classes ) {
  global $post;
  foreach ( ( get_the_category( $post->ID ) ) as $category ) {
    $classes[] = $category->category_nicename;
  }
  return $classes;
}
add_filter( 'post_class', 'category_id_class' );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question