1

I'm hoping that Its just my tired eyes missing something and a fresh pair of eyeballs might catch what I'm missing.

I have a custom taxonomy with a slug of 'residential_project_types' that is assigned to a custom post type of residential_projects. I want to display all terms from the taxonomy, outputting the term name and link.

Its KIND OF working...

Instead of displaying a single term for each, it appears to be displaying a term for every post included in the term. This of course is creating duplicates. In addition, the HTML isn't displaying properly, causing weird overlapping of elements.

My hunch is something is messed up with the loop...? Haven't been able to figure it out though. Any and all helped is much appreciated!

Here's a link to the broken/buggy page: http://desarch.robertrhu.net/residential/

Here's the code I've written:

<?php
    $terms = get_terms( array(
        'taxonomy'   => 'residential_project_types',
        'orderby'    => 'count',
        'hide_empty' => false,
        'fields'     => 'all'
    ) );
?>

<?php
    foreach( $terms as $term ) {

    $args = array(
        'post_type' => 'residential_projects',
        'residential_project_types' => $term->slug
    );

    $term_link = get_term_link( $term );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) :
        /* Start the Loop */
        while ( $query->have_posts() ) : $query->the_post(); ?>
        <a class="property-thumb-link"
           href="<?php echo $term_link; ?>">
            <div class="property-thumb column medium-6 small-12">

                <img src="<?php the_field('category_image', $term); ?>"
                     alt="<?php the_field ('category_image_alt', $term); ?>" />

                <div class="property-thumb-title">
                    <h2>
                        <?php echo $term->name; ?>
                    </h2>
                </div>
            </div>
        </a>
     <?php wp_reset_postdata();
    endwhile;
 endif; }?>
  • in your code you loop over projects. then you want to display all projects in very taxonomies and not only all taxonomies ? – mmm Apr 25 '17 at 20:16
  • I want to display any terms in the taxonomy that have projects assigned to them. BUT, I only want to display the term one time on the page. No duplicates. – user5176291 Apr 25 '17 at 20:24
2

as mentioned by @mmm you loop over the terms and in each term you loop over each project – however I think this is what you wanted to do:

$terms = get_terms( array(
    'taxonomy'   => 'residential_project_types',
    'orderby'    => 'count',
    'hide_empty' => true
) );

foreach( $terms as $term ) :
?>
    <a class="property-thumb-link"
       href="<?php echo get_term_link( $term ); ?>">
        <div class="property-thumb column medium-6 small-12">

            <img src="<?php the_field('category_image', $term); ?>"
                 alt="<?php the_field ('category_image_alt', $term); ?>" />

            <div class="property-thumb-title">
                <h2>
                    <?php echo $term->name; ?>
                </h2>
            </div>
        </div>
    </a>
<?php
endforeach;
  • Thank you for the response! Was your code cut off? – user5176291 Apr 25 '17 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.