that looks like pieces of a conditional tag from a template file. Where did you get it and what is it supposed to do?
“The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches.”
Thread Starter
Gustav
(@4ever16)
@janet4now
So when i click on a category lets say category named “birds” what condition is called?
Cause it’s not “is_category()” or “is_archive()”
-
This reply was modified 5 years, 3 months ago by
Gustav.
You skipped my questions….
that looks like pieces of a conditional tag from a template file. Where did you get it and what is it supposed to do?
Thread Starter
Gustav
(@4ever16)
@janet4now it should do something and it does in home front page single page search page BUT NOT when i click category/when im visiting a category/clicking category.
Can you post a link to the page you’re talking about where you have a category listed?
Code in functions.php executes too early for is_*() checks to work correctly. They work great on templates, not in functions.php. The only way for such code to work in functions.php is if they execute within an action callback where the related action fires late enough for such checks to work as expected.
Thread Starter
Gustav
(@4ever16)
So how do i fix it?
This is the complete code.
function add_contributert_class_to_single_post( $classes ) {
if ( is_page() || is_category() || is_archive() || is_tag() || is_search() || is_front_page() || is_home() || is_single() ) {
$post_id = get_queried_object_id();
$author_ID = get_post_field( 'post_author', $post_id );
$author_data = get_userdata( $author_ID );
if (in_array( 'contributor', $author_data->roles)) {
// add post class
array_push( $classes, 'user-contributor' );
}
}
return $classes;
}
add_filter( 'post_class', 'add_contributert_class_to_single_post' );
-
This reply was modified 5 years, 3 months ago by
Gustav.
IDK, your code works for me on my site. The ‘user-contributor’ class appears in every <article> tag of any post written by a contributor.
Thread Starter
Gustav
(@4ever16)
@bcworkz also when you click a category which lists all posts in a category?
Thread Starter
Gustav
(@4ever16)
Ok so i now what the problem is.
The category page is missing <?php post_class(); ?>
Every page has <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
-
This reply was modified 5 years, 3 months ago by
Gustav.
There are a number of such lists possible. The list when you follow a link like /category/foo/ is what I checked against. Success does depend upon the theme calling post_class(). Most do, but it’s not required. If yours doesn’t your choices are to modify the template, find a different hook, or use a different theme.