link directly to custom post if there is only one in the taxonomy

I have this code in my custom-taxonomy.php file.

        if ( have_posts() ) :

            //do_action( 'generate_archive_title' );
            $term = get_queried_object();
            $term_id = $term->term_id;
            $taxonomy_name = $term->taxonomy;
            $termchildren = get_terms( array(
                'taxonomy' => $taxonomy_name,
                'parent' => $term_id
            ));
            $secondimg = get_field('secondary_img', $term);
            ?>

            <header class="page-header">
                <h1 class="page-title"><?php single_cat_title(); ?></h1>
                <?php
                if ( ! empty( term_description() ) ) :
                    printf( '<div class="taxonomy-description">%s</div>', term_description() );
                endif;
                if ( $secondimg ) echo '<img src="'.$secondimg['url'].'" alt="our image">';
                ?>
            </header>

            <?php

            if ( $termchildren  ) {
                // we have kids...just show the terms.
                $howmany = count($termchildren);
                $i=1;
                foreach ( $termchildren as $child ) {
                    if ($i % 2 == 0) {
                        echo '<div class="tax-entry flex '.$howmany.'">';
                    } else {
                        echo '<div class="tax-entry flex '.$howmany.'" style="flex-direction: row-reverse;">';
                    }
                    ?>
                        <figure class="wp-block-media-text__media">
                            <?php if ( get_field('product_category_img', $child) ) echo wp_get_attachment_image( get_field('product_category_img', $child), 'full' ) ;?>
                        </figure>
                        <div style="flex:60% 0 0;" class="wp-block-media-text__content">
                            <h2><?php echo $child->name ;?></h2>
                            <p>
                                <?php
                                if (get_field('short_description', $child )){ 
                                    echo get_field('short_description', $child );
                                }else{
                                    echo $child->description;
                                }?>
                            </p>
                            <a class="button" href="<?php echo get_term_link( $child, $taxonomy_name ) ;?>">See this Series</a>

                        </div>
                    </div> <!-- END FLEX -->
                    <?php
                    $i++;
                }
            } else { 
                // no kids...show the products
                echo '<div class="productCategories grid">';
                while ( have_posts() ) : the_post();
                    ?>

                    <div class="product_cat">
                        <a href="<?php the_permalink();?>">
                            <?php the_post_thumbnail('small');?>
                            <h2><?php the_title();?></h2>
                <?php
                $subtitle = the_field('subtitle');
                if ( $subtitle ) { 
                  echo '<h3 class="entry-subtitle">'.$subtitle.'</h3>';
                }
                 ?>
                        </a>
                        <?php //if (get_field('specs')['part_number']) echo '<span>'.get_field('specs')['part_number'].'</span>';?>
                    </div>

                <?php
                endwhile;
                echo '</div>';
            }

            /**
             * generate_after_loop hook.
             *
             * @since 2.3
             */
            do_action( 'generate_after_loop' );

            generate_content_nav( 'nav-below' );

        else :

            get_template_part( 'no-results', 'archive' );

        endif;

I’m trying to figure out if there is a method to change the link on this line:

<a class="button" href="<?php echo get_term_link( $child, $taxonomy_name ) ;?>">See this Series</a>

so that if the term only has one product it will link to that product itself.

Is this possible?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

How about doing it this way:

<?php
// Get (at most) 2 "product" posts in the child term.
$posts = get_posts( array(
    'post_type'      => 'product', // just change the post type if it is not "product"
    'posts_per_page' => 2,
    $taxonomy_name   => $child->slug,
) );

// If there's exactly 1 post, use the post permalink.
if ( 1 === count( $posts ) ) : ?>
    <a class="button" href="<?php the_permalink( $posts[0] ); ?>">See this Product</a>
<?php // Else, use the term link.
else : ?>
    <a class="button" href="<?php echo esc_url( get_term_link( $child, $taxonomy_name ) ); ?>">See this Series</a>
<?php endif; ?>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x