0
votes

Ok so my php is pretty bad, so it may be hard to understand what I mean at some points but pretty much I have a wordpress loop and at one point I get the category of the post and echo it into the class.

Some posts have multiple categories and I want to echo them both into the class.

So far this is what I use to get the categories

$categories = get_the_category(); 

and this is how I echo them

<?php echo $categories[0]->category_nicename; ?>

If I change to

<?php echo $categories[1]->category_nicename; ?> 

it gets the second category but I want it to get all of them

This is the full code

<?php query_posts("post_type=portfolio"); ?>
<?php $i=0; /** start the project loop here */?>
<?php if(have_posts()):?>
<?php while(have_posts()) : the_post();?> 
<?php $i++; ?>

<?php
$image1ID = get_field('thumbnail');
$image1 = wp_get_attachment_image_src( $image1ID, '500by250-thumb' );
$attachment = get_post( $image1ID );
$image1_title = $attachment->post_title;
$categories = get_the_category(); 

?>          

    <pre>
        <?php print_r($categories) ?>
    </pre>

    <li data-type="<?php echo $categories[0]->category_nicename; ?>" data-id="id-<?php echo $i ?>" class="portfolio-thumb <?php the_field('thumb_size');?>">

        <a data-scroll="#portfolio-second" href="<?php the_permalink(); ?>" title="<?php echo $image_title; ?>">
            <div class="hover-content">
                <span class="thumb-caption"><?php the_field('thumb_caption');?></span>
                <span class="thumb-title"><?php the_title();?></span>
            </div>
            <img src="<?php echo $image1[0] ?>" alt="<?php echo $image_title; ?>">
        </a>

    </li>   

<?php endwhile; ?>
<?php else: ?>  
<?php endif; ?>
<?php wp_reset_query(); ?>
1

1 Answers

2
votes

If you want to loop through all categories array, you can use foreach statement like below:

<?php foreach($categories as $category){?>
    <li data-type="<?php echo $category->category_nicename; ?>" data-id="id-<?php echo $i ?>" class="portfolio-thumb <?php the_field('thumb_size');?>">

                        <a data-scroll="#portfolio-second" href="<?php the_permalink(); ?>" title="<?php echo $image_title; ?>">
                            <div class="hover-content">
                                <span class="thumb-caption"><?php the_field('thumb_caption');?></span>
                                <span class="thumb-title"><?php the_title();?></span>
                            </div>
                            <img src="<?php echo $image1[0] ?>" alt="<?php echo $image_title; ?>">
                        </a>

                    </li> 
<?php } ?>

The full code would be:

        <?php query_posts("post_type=portfolio"); ?>
        <?php $i=0; /** start the project loop here */?>
        <?php if(have_posts()):?>
        <?php while(have_posts()) : the_post();?> 
        <?php $i++; ?>

        <?php
        $image1ID = get_field('thumbnail');
        $image1 = wp_get_attachment_image_src( $image1ID, '500by250-thumb' );
        $attachment = get_post( $image1ID );
        $image1_title = $attachment->post_title;
        $categories = get_the_category(); 

        ?>          

            <pre>
                <?php print_r($categories) ?>
            </pre>

            <?php foreach($categories as $category){?>
    <li data-type="<?php echo $category->category_nicename; ?>" data-id="id-<?php echo $i ?>" class="portfolio-thumb <?php the_field('thumb_size');?>">

                        <a data-scroll="#portfolio-second" href="<?php the_permalink(); ?>" title="<?php echo $image_title; ?>">
                            <div class="hover-content">
                                <span class="thumb-caption"><?php the_field('thumb_caption');?></span>
                                <span class="thumb-title"><?php the_title();?></span>
                            </div>
                            <img src="<?php echo $image1[0] ?>" alt="<?php echo $image_title; ?>">
                        </a>

                    </li> 
<?php } ?>
        <?php endwhile; ?>
        <?php else: ?>  
        <?php endif; ?>
        <?php wp_reset_query(); ?>