- This topic has 22 replies, 3 voices, and was last updated 2 years, 7 months ago by
David.
-
AuthorPosts
-
May 12, 2015 at 12:06 pm #107502
anamoore
Hy!
I have GP premium.
I use to clasify my articles by big categories or pages. I use only one header background for the entire site, but I want to have a representative site header image for each category when you click on it, and for each article from that category. Same to pages. How to do that?
May 12, 2015 at 2:30 pm #107569Tom
Lead DeveloperLead DeveloperYou can use CSS like this:
.category-xx .site-header { background-image: url('CUSTOM IMAGE URL'); }
Replace xx with the ID of that specific category.
Let me know if that helps or not π
May 13, 2015 at 12:37 am #107624anamoore
Hey
It worked, but only for category blog page. I also want it set for the articles that are in it, when you click on them. I mean, for the articles that have the main category setted as ex: “Cars”.
May 13, 2015 at 12:51 am #107627anamoore
I also need the code for the pages.
May 13, 2015 at 7:38 am #107675Tom
Lead DeveloperLead DeveloperWordPress doesn’t add the category class to the single posts (for some reason).
You can manually add the classes to your single posts using a function like this: https://wordpress.org/support/topic/modify-body_class-output-to-show-category-slug-for-single-posts?replies=11#post-1396482
As for pages, it’s very similar to the above:
body.page-id-xx { background-image: url(''); }
May 16, 2015 at 2:12 am #108231anamoore
Hi, Tom!
Unfortunately, I cant handle this…
I tried inserting<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
in Index.php and then insertingcategory-cars {background: red;}
in CSS Editor, but something its missing…First, on Homepage, there is a little background around the excerpts of posts from that category and I dont want that.
Then, I click on the excerpt to view the post. Here, the post has the main background of the site, not the background from the command
category-cars {background: red;}
in CSS Editor`…May 16, 2015 at 4:59 pm #108374Tom
Lead DeveloperLead DeveloperShouldn’t have to insert anything into index.php..
This is the code you want to add into your child theme functions.php:
add_filter('body_class','add_category_to_single'); function add_category_to_single($classes, $class) { if (is_single() ) { global $post; foreach((get_the_category($post->ID)) as $category) { echo $category->cat_name . ' '; // add category slug to the $classes array $classes[] = 'category-'.$category->slug; } } // return the $classes array return $classes; }
Then your CSS should work π
May 17, 2015 at 3:51 am #108460anamoore
CSS stil don’t work. I think it should be somethhing like “if it’s single and in category 7 then take category site-header”, but don’t know how to write it.
May 17, 2015 at 8:23 am #108506Tom
Lead DeveloperLead DeveloperThe above code if added to your functions.php file should add the category name to the body class on single posts.
Can you link me to your site so I can see if the code is working?
May 17, 2015 at 8:41 am #108512anamoore
1. Click on Cars category in menu. You will see a particular Mercedes header and a dark purple background. 2. Click on a post excerpt, you won’t see that header and background anymore. π
May 17, 2015 at 11:40 pm #108575Tom
Lead DeveloperLead DeveloperIn your CSS, you have the custom class set to: .category-7
Instead, you should use: .category-cars
Then it should work.
Let me know π
May 18, 2015 at 5:35 am #108639anamoore
Yay! It worked!
Thank you very much, again, and sorry for stressing you πMay 18, 2015 at 8:42 am #108667Tom
Lead DeveloperLead DeveloperNo stress! π
July 13, 2015 at 4:04 am #120576anamoore
Hello!
I updated the theme, and the code doesn’t work anymore.
The code for functions.php was:
add_filter('body_class','add_category_to_single'); function add_category_to_single($classes, $class) { if (is_single() ) { global $post; foreach((get_the_category($post->ID)) as $category) { echo $category->cat_name . ' '; // add category slug to the $classes array $classes[] = 'category-'.$category->slug; } } // return the $classes array return $classes; }
I got some text on the header like: Warning: Missing argument 2 for add_category_to_single() in /home/roxiro5/public_html/roxirose.com/wp-content/themes/generatepress/functions.php on line 598
Cars class=”single single-post postid-2006 single-format-standard logged-in admin-bar no-customize-support post-image-above-header post-image-aligned-center secondary-nav-above-header secondary-nav-aligned-right featured-image-active no-sidebar nav-below-header fluid-header separate-containers active-footer-widgets-1 nav-aligned-center header-aligned-center category-cars”>what should I do?
July 13, 2015 at 9:31 am #120674Tom
Lead DeveloperLead DeveloperTry this:
add_filter('body_class','add_category_to_single'); function add_category_to_single($classes) { if (is_single() ) { global $post; foreach((get_the_category($post->ID)) as $category) { echo $category->cat_name . ' '; // add category slug to the $classes array $classes[] = 'category-'.$category->slug; } } // return the $classes array return $classes; }
-
AuthorPosts
- You must be logged in to reply to this topic.