This work but now I need convert in function…
$cat = get_the_category($post->post_parent);
$myCat = $cat[0]->cat_ID;
if ($myCat == 001) {
$style = ‘category-001’;
}
elseif ($myCat == 002) {
$style = ‘category-002’;
}
echo $style;
Ideas, uh? 🙂
If you amended your category template to include an additional unique class based on the category name or id, you could then style your images using that class. Eg:
<?php $cat_class = 'cat_'.get_cat_ID( single_cat_title());?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class($cat_class);?>>
That’s just off the top of my head (ie untested) but, in theory, it should output classes such as:
cat_3
cat_7
which you could then style using:
.cat_3 img {...}
esmi, your function return the default category not the post parent category. 🙁
Sorry – I mis-read what you needed. But it might still be possible if you add some extra steps using get_category_parents.
<?php $catid = get_cat_ID( single_cat_title();
$parents = explode('|',get_category_parents($catid, FALSE, '|'));
$cat_class = 'cat_'.$parents[0];?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class($cat_class);?>>
Give me this error: syntax error, unexpected ‘;’
And y I chango to $catid = get_cat_ID( single_cat_title());
This other: Catchable fatal error: Object of class WP_Error could not be converted to string
This piece of code works to me… is possible transform it in a function?
<?php
$cat = get_the_category($post->post_parent);
$catid = $cat[0]->cat_ID;
//$catid = get_the_category( single_cat_title() );
$parents = explode('|',get_category_parents($catid, FALSE, '|','nicename'));
$cat_class = $parents[0];
?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class($cat_class);?>>