How to display posts categories as classes

Seems that I am just too stupid to achieve this. Seems so simple, yet…
I created a template and try to display a custom portfolio (registered by a custom post plugIn) This works fine with the following code:

<div id="container">

    <?php

    //Define your custom post type name in the arguments                           
    $args = array('post_type' => 'boxes_scientists');

    //Define the loop based on arguments                            
    $loop = new WP_Query( $args );               

    //Display the contents                            
    while ( $loop->have_posts() ) : $loop->the_post();
    ?>

    /*
    <?php
        foreach((get_the_category()) as $category) { 
            echo $category->cat_name . ' '; 
    } 
    ?>

    <?php the_category(', '); ?>
    */

    <div class="some_base_class [categories of the post need to go here]">

        <a class="element" href="<?php the_permalink(); ?>"></a>
        <div class="portfolio-box">
            <div class="portfolio-naming">
                <h2 class="portfolio-title"><?php the_title(); ?></h2>
                <h3 class="portfolio-attributes"><?php the_content(); ?></h3>
          </div>
        </div>

        <?php the_post_thumbnail(); ?>

    </div>

<?php endwhile;?>

Read More

but I can’t get to work the commented out code part and display the categories inside the class tag of my container element (for each post displayed in the loop).

I actually found also this concept:

https://lorelle.wordpress.com/2007/09/06/using-wordpress-categories-to-style-posts

which seemed to be exactly what I need but sadly this didn’t work at all for me. (placed the function inside functions.php on my child theme and theme both without any result)

What I am misunderstanding here? Can someone show me the correct code I have to use? Would be so awesome. Thanks in advance!

EDIT

So this finally brings me on the right way:

<?php
[…]
$category = get_the_category();
$firstCategory = $category[0]->cat_name;?>



<div class="some_base_class <?php echo $firstCategory ?>">

I missed the “echo” thing inside my div. Now I will have to find the way to display all categories of my post instead of only the first.

EDIT 2:

<div id="boxes_section" class="main-content master-section-content nano_boxes no-detect no-padding">
        <div class="container">
            <div class="row">
                <div class="col-md-12 normal-column start-animated-content az-fade-in" data-delay="300">
                    <div class="blank-divider" style="height: 30px;"></div>

                    <div id="portfolio-item-section" class="portfolio-output masonry-ly-portfolio classic-module no-pagination" data-cols="3">

                        <?php

                        //Define your custom post type name in the arguments                           
                        $args = array('post_type' => 'boxes_scientists');

                        //Define the loop based on arguments                            
                        $loop = new WP_Query( $args );               

                        //Display the contents                            
                        while ( $loop->have_posts() ) : $loop->the_post();
                        ?>


                        <div class="single-portfolio-item az-col-full-width-4 [NEED THE CLASSES HERE]">


                              <a class="classic-portfolio-box normal-type-prt" href="<?php the_permalink(); ?>">
                                  <p class="site_leave"><i class="font-icon-forward"></i>You are going to leave this website</p>
                              </a>
                              <div class="portfolio-box">
                                  <div class="portfolio-naming">
                                      <h2 class="portfolio-title"><?php the_title(); ?></h2>
                                      <h3 class="portfolio-attributes"><?php the_content(); ?></h3>
                                  </div>
                              </div>

                              <?php the_post_thumbnail(); ?>

                        </div>

                        <?php endwhile;?>

                    </div>
                </div>
            </div>
        </div>
    </div>

Related posts

1 comment

  1. I just write code category at functions.php like this:

    function sps_category(){
        $categories = get_the_category();   
        foreach ( $categories as $category ) {
            echo '<a href="'.esc_url( get_category_link( $category->term_id ) ).'">
                        '.esc_html( $category->cat_name ).'
                </a>';
        }
    }
    

    and i call my functions at my page

    <?php sps_category() ?>
    

    but in other ways,
    you can write your code in class like this:

    $categories = get_the_category();   
    foreach ( $categories as $category ) {
        echo '<div class=".esc_attr($category->cat_name)."><a href="'.esc_url( get_category_link( $category->term_id ) ).'">
                    '.esc_html( $category->cat_name ).'
                </a><div>';
        }
    

    It can editable and dynamic if you want to show just 1 category.
    And dont forget to use escape function if you put some php variabel/functions if you write in attribut html
    for example use esc_attr(somecode) if it class/title/name/id attribute.

Comments are closed.