ThemeShaper Forums » Thematic

[closed]

Custom post title class Based on Category

(3 posts)
  • Started 9 years ago by almour
  • Latest reply from almour
  • This topic is resolved
  1. almour
    Member

    Hi, I would like to have custom post title styling for different categories in my child theme.

    For example: Post title on category a >> css: class .a
    Post title on category b >> css: class .b

    In order to do that, I think I have to filter thematic_postheader_posttitle so it generates custom class, and then styling each class in CSS.

    My code:

    function ruby_postheader_posttitle($posttitle){
    	if(in_category('cat')){
    		$posttitle = '<h2 class="catclass"><a href="';
    	    $posttitle .= apply_filters('the_permalink', get_permalink());
    	    $posttitle .= '" title="';
    	    $posttitle .= __('Permalink to ', 'thematic') . the_title_attribute('echo=0');
    	    $posttitle .= '" rel="bookmark">';
    	    $posttitle .= get_the_title();
    	    $posttitle .= "</a></h2>\n";
    		}
    	else if(in_category ('dog')){
    		$posttitle = '<h2 class="dogclass"><a href="';
    	    $posttitle .= apply_filters('the_permalink', get_permalink());
    	    $posttitle .= '" title="';
    	    $posttitle .= __('Permalink to ', 'thematic') . the_title_attribute('echo=0');
    	    $posttitle .= '" rel="bookmark">';
    	    $posttitle .= get_the_title();
    	    $posttitle .= "</a></h2>\n";
    		}
    	else{
    		return $posttitle;
    		}
    }?>
    <?php add_filter('thematic_postheader_posttitle', 'ruby_postheader_posttitle');
    ?>

    But I ended up with no title in both categories :P. What am I doing wrong?

    Posted 9 years ago #
  2. Vasudeva Adiga
    Member

    You don't have to write php code to do this: each post has the category css class already like this:

    <div class="post-1 post type-post hentry category-blog category-uncategorized" id="post-1"><h2 class="entry-title">Hello world!</h2>....

    Write CSS like this:

    div.category-uncategorized h2 {
    color: red;
    }

    div.category-cat h2 {
    color: white;
    }

    div.category-dog h2 {
    color: black;
    }

    Posted 9 years ago #
  3. almour
    Member

    Thank you!

    You are right. Just a little correction though, it is h1 instead of h2.. makes me wonder.. (I also thought it is suppose to be h2)

    Posted 9 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.