• Hi !

    I’m trying to asign styles to images into galleries by categories (trhough out image.php) but WP asign default category to each image.

    I thinks that perhaps making an array with category post parent could be a solution but I’m not a programmer.

    May somebody help me? Please excuse my poor english but I’m speaker spanish ??

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter Daniel

    (@fape)

    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 {...}

    Thread Starter Daniel

    (@fape)

    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);?>>
    Thread Starter Daniel

    (@fape)

    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

    Thread Starter Daniel

    (@fape)

    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);?>>
    Thread Starter Daniel

    (@fape)

    Suggestions? Ideas? uh?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Styling images by categories’ is closed to new replies.