• Hi there…

    Some Details:

    1. I have a custom post type with a slug called ‘residential.

    2. There’s a taxonomy associated with this custom post type with a slug of ‘residential_project_type’

    3. Inside this taxonomy there are several terms

    4. I’ve created a page template where I’ve created a tax_query

    What I’m trying to accomplish:

    I want to display all terms from this taxonomy on a page, outputting specific elements of the terms into an HTML layout such as…

    1. The term title
    2. The permalink to the term archive page
    3. And a custom image field attached to each term

    I’ve built it out as makes sense to me but no luck getting the terms to display.

    Thanks in advance for your help!

    Here’s the code I have:

    `<?php
    /*
    * Residential Projects Types Archive Template
    * Description: Template for Residential Project Types archive
    */

    get_header(); ?>

    <?php
    $the_query = new WP_Query( array(
    'post_type' => 'residential',
    'tax_query' => array(
    array (
    'taxonomy' => 'residential_project_types',
    'field' => 'name',
    ),
    ),
    ) );
    ?>

    <div id="main-content">
    <?php do_action( 'foundationpress_after_header' ); ?>

    <?php get_template_part( 'assets/partials/partial', 'mobilenav' ); ?>

    <?php get_template_part( 'assets/partials/partial', 'headings' ); ?>

    <?php do_action( 'foundationpress_before_content' ); ?>
    <?php if ( $the_query->have_posts() ) : ?>
    <?php /* Start the Loop */ ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <a class="property-thumb-link"
    href="<?php the_permalink($post->ID); ?>">
    <div class="property-thumb column medium-6 small-12">

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

    <div class="property-thumb-title">
    <h2>
    <?php the_title($post->ID); ?>
    </h2>
    </div>
    </div>
    </a>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; // End have_posts() check. ?>
    <?php do_action( 'foundationpress_after_content' ); ?>

    </div>

    <?php get_footer(); ?>`

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The “tax_query” in your code is for restricting which posts are found in a post query based on taxonomy terms assigned.

    To get terms use get_terms() 🙂
    You’ll get an array of term objects which you can loop through and generate output using WP_Term properties and other WP functions as needed.

    Thread Starter robertrhu

    (@robertrhu)

    Thank you for the response!

    Still been working on getting this right. I’ve updated my code to use get_terms. Its almost working. Now it displays duplicates of the terms on the archive page. There is a term listed for every post assigned to the term.

    Here’s a link to the page: http://desarch.robertrhu.net/residential/

    Here’s the current code:

    <?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 endwhile;
            wp_reset_postdata();
         endif; }?>
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Display All Terms of Taxonomy in Page Template’ is closed to new replies.