• Resolved Ericka125

    (@ericka125)


    I’m trying to develop a portfolio page for myself, and I would like to do different things with the tags & categories based on their names.

    The page I’m working on is this: http://erickaseastrand.com/portfolio/
    The PHP file I have is this: http://erickaseastrand.com/wp-content/themes/slightlyoffkilter/portfolio.php

    The forums I’ve been looking at are confusing, but this is what I have so far.

    <div id="portfoliogrid">
    
    <?php endwhile; ?>
    <?php //The Query
     query_posts('category_name=portfolio&posts_per_page=10'); ?>
             <?php //The Loop
    if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>  
    
        	<div class="portfoliobox <?php the_category ?>">
    
                <a href="<?php the_permalink() ?>"><img src="<?php the_field('thumbnail') ; ?>"></a>
                <h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
                <p><?php the_field('excerpt') ; ?> <a href="<?php the_permalink() ?>">Read More...</a></p>
                <?php the_tags('<p class="tags"><span class="<?php body_class($class); ?>">', '</span><span <?php body_class($class); ?>>' , '</span></p>'); ?> 
    
    		</div> 
    
    <?php endwhile; else: endif;?>
    <?php //Reset Query
    wp_reset_query(); ?>            
    
       </div>

    What I would like to do is the following:

    <span class="school">School</span>

    Where school is the tag slug and School is the friendly name.

    I would like to to do this for each individual tag within each individual query post.

    Then, similarly, I’d like to classify the portfoliobox div as a certain category, ie the following:

    <div class="portfoliobox graphic-design">...</div>

    Where graphic-design is the category slug.

    The codex seems unclear in these sorts of examples so if someone could explain to me how mine would be directly implemented via their own words or a different website, that would be ideal.

    Thank you for any help you can provide.

Viewing 6 replies - 1 through 6 (of 6 total)
  • post_class() would do something like your ides, but would also add way more css classes.
    http://codex.wordpress.org/Function_Reference/post_class

    for your idea:

    <div class="portfoliobox <?php the_category ?>">

    you better work with get_the_category() and a foreach loop;
    http://codex.wordpress.org/Function_Reference/get_the_category

    example:

    <div class="portfoliobox <?php foreach( get_the_category() as $cat ) { echo $cat->slug . '  '; } ?>">
    Thread Starter Ericka125

    (@ericka125)

    Great! That definitely fixed my category problem, but I’m still have trouble with the tag classes appearing the way I want them to.

    i missed that bit with the tags – what are you trying to do there?

    <?php the_tags('<p class="tags"><span class="<?php body_class($class); ?>">', '</span><span <?php body_class($class); ?>>' , '</span></p>'); ?>

    body_class() does not seem to be suitable, and your way of integrating is not correct as well.

    can you describe with some words how you want css classes with the tags?

    Thread Starter Ericka125

    (@ericka125)

    What I would like to do is the following:

    <span class="school">School</span>

    Where “school” is the tag slug and “School” is the name.

    I would like to to do this for each individual tag within each individual query post.

    If that’s still unclear I can think of another way to word it.

    that made it clear;
    possible use get_the_tags() with a foreach loop instead of the_tags();

    example (for linked tags):

    <?php $tags = get_the_tags();
    if( $tags ) : ?>
      <p class="tags">
      <?php foreach( $tags as $tag ) { ?>
       <span class="<?php echo $tag->slug; ?>"><a href="<?php echo get_tag_link($tag->term_id); ?>"><?php echo $tag->name; ?></a></span>
      <?php } ?>
    </p>
    <?php endif; ?>

    just be aware, as tags can have random names/slugs, that there is a risk of confusion with existing css classes of the theme.

    http://codex.wordpress.org/Function_Reference/get_the_tags
    http://codex.wordpress.org/Function_Reference/get_tag_link

    Thread Starter Ericka125

    (@ericka125)

    Superb! Thank you so much!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Adding Tag/Category Name as CSS Class in Query’ is closed to new replies.