jcadima
1/9/2019 - 10:53 PM

Get list of categories from taxonomy


<?php
// LOOP FOR CATEGORY OF TYPE: PROJECT
// Post Type: project
// Taxonomy for 'project':  project_category

if ( get_sub_field( 'project_grid') ) :

    // Get all Cats
    $all_cats = get_terms( ['taxonomy' => 'project_category'] ) ;

//    echo '<pre>';
//    print_r( $all_cats );
//    echo '</pre>';
//      foreach ( $all_cats as $cat ) :
//        echo 'ID: ' . $cat->term_id . '<br>' ;
//        echo 'Name: ' . $cat->name . '<br>' ;
//        // Get the ID of a given category
//        $category_link = get_category_link( $cat->term_id );
//        echo 'Category Permalink: ' . $category_link . '<br>'  ;
//
//        if ( get_wp_term_image( $cat->term_id ) )
//            echo 'Image URL: ' .  get_wp_term_image( $cat->term_id ) . '<br>'  ;
//        else
//            echo 'Using Default Placeholder<br>' ;
//
//        echo '<hr>' ;
//        endforeach ;
?>

        <section class="project-grid">
            <div class="row">
                <?php foreach ( $all_cats as $cat ) :
                    $category_link = get_category_link( $cat->term_id );  ?>
                    <div class="project-col col-lg-3 col-md-6 col-sm-12">

                        <div class="">
                            <strong><?php echo $cat->name;  ?></strong>
                            <a class="post-thumbnail" href="<?php echo $category_link; ?>" aria-hidden="true" tabindex="-1">
                                <?php

                                if ( get_wp_term_image( $cat->term_id ) ) : ?>
                                    <img src="<?php echo get_wp_term_image( $cat->term_id ) ; ?>">
                                <?php
                                else :
                                    echo '<img width="600" height="500" src="' . esc_url( 'https://via.placeholder.com/600x500/ddd/666.png?text=No+Project+Media', 'understrap' ) . '" alt="' . esc_attr__( 'Blank Profile Picture', 'understrap' ) . '" />';
                                endif;
                                ?>
                            </a>
                        </div>

                    </div>
                <?php endforeach ; ?>

            </div>
        </section>



<?php  endif; ?>