• Hello, i have 5 different class for each post type:
    <li data-id="id-1" class="web">
    and this class is depends on category for example if i post in photo category class should change to class=”photo”
    i need to know how to control them and let it decided about which class is for which category?

    thanks

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    try conditional tags

    <?php if(is_category('9')) { $class='photo'; } ?>
    <li data-id="id-1" class="<?php echo $class; ?>">

    If the category ID of your photo category is 9.

    Thread Starter aghahamidgol

    (@aghahamidgol)

    thank you keesiemeijer, but i have 5 class i have to write else if for them or switch case?
    how to manage them? i have 5 different category for post

    Moderator keesiemeijer

    (@keesiemeijer)

    you can do it 5 times like so

    <?php
    if(is_category('9')) { $class='photo'; }
    if(is_category('6')) { $class='web'; }
    if(is_category('8')) { $class='news'; }
    if(is_category('12')) { $class='etc'; }
    if(is_category('17')) { $class='whatever'; }
    ?>
    <li data-id="id-1" class="<?php echo $class; ?>">

    Thread Starter aghahamidgol

    (@aghahamidgol)

    tnx a lot i am trying now

    Thread Starter aghahamidgol

    (@aghahamidgol)

    in output class is empty πŸ™ it doesn’t work πŸ™

    <?php if(is_category('5')) { $class='web'; }
    			 if(is_category('7')) { $class='video'; }
    			if(is_category('8')) { $class='graphic'; }
    			if(is_category('6')) { $class='photo'; }
    			if(is_category('9')) { $class='commerce'; }
    			if(is_category('10')) { $class='coding'; }
    	  ?>
    	<li data-id="id-1" class="<?php echo $class; ?>">

    this is all the code for loop

    <!-- Begin loop porfolio -->
    <?php query_posts('cat=4'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    		  <?php if(is_category('5')) { $class='web'; }
    			 if(is_category('7')) { $class='video'; }
    			if(is_category('8')) { $class='graphic'; }
    			if(is_category('6')) { $class='photo'; }
    			if(is_category('9')) { $class='commerce'; }
    			if(is_category('10')) { $class='coding'; }
    	  ?>
    	<li data-id="id-1" class="<?php echo $class; ?>">
            <span class="boxgrid caption">
            	<!-- Begin Full Size Link and Description -->
    			<a href="<?php bloginfo('stylesheet_directory') ?>/style/images/1.jpg" class="colorbox" title="<?php the_title(); ?>">
    				<img src="<?php bloginfo('stylesheet_directory') ?>/style/images/th1.jpg"/><!-- Image Thumnail -->
    			</a>
            	<!-- End Full Size Link and Description -->
            	<!-- Begin Caption -->
            	<span class="cover boxcaption">
          			<strong><?php the_title(); ?></strong>
            		<em><a href="<?php the_permalink(); ?>">Click here to visit the details.</a></em>
            	</span><!-- End Caption -->
            </span>
         </li>
    
    <?php endwhile; else: ?>
    <p>error</p>
    <?php endif; ?>
    
    <!-- End loop porfolio -->
    Moderator keesiemeijer

    (@keesiemeijer)

    In this loop you query the loop for category 4 so you don’t have to test for the category. Or put this in there:
    if(is_category('4')) { $class='porfolio'; }

    Thread Starter aghahamidgol

    (@aghahamidgol)

    right , my main category is portfolio with id 4 and i need to make query just for this category, and those number are id of sub-category.
    shall i change the number of sub-category to name or something else :(?
    now class change to “portfolio” the main cat it means it works but how to manage them using with sub-cat?

    Moderator keesiemeijer

    (@keesiemeijer)

    try this.
    change this:

    <?php if(is_category('5')) { $class='web'; }
    			 if(is_category('7')) { $class='video'; }
    			if(is_category('8')) { $class='graphic'; }
    			if(is_category('6')) { $class='photo'; }
    			if(is_category('9')) { $class='commerce'; }
    			if(is_category('10')) { $class='coding'; }
    	  ?>
    	<li data-id="id-1" class="<?php echo $class; ?>">

    to only this: <li data-id="id-1" <?php post_class(); ?>>
    now you get something like this: <li data-id="id-1" class="category-photo">
    if the post has the category photo

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘how to switch to diffrent classes’ is closed to new replies.