FINAL EDIT
From comments
Unfortunately when I try method one, all links go back to the homepage
As I stated, everything works on my test install. I actually forgot that your code works for you, because you have hardcoded the URL
<a href='/?$tax=$slug' ><h5> $term->name </h5></a>
What this tells me is that get_term_link()
is not returning the expected URL which 404's which gets redirected to the home page. This is caused, in all probability, because you did not flush your permalinks after you have created your taxonomy, or you have a permalink issue. You should sort that out first, and use my code in option 1 as benchmark. As I said, my code is now tested and everything is working as expected, so it should work for you as well
Just a tip, if something does not work, don't hide an issue the way you did, fix it. Simply hiding an issue can cause other unexpected stuff later on ;-)
ORIGINAL ANSWER
There are basically two ways to do this
Inside your shortcode where you check the post count for the term and then query the single post and then showing that link instead of the category link
Using the template_redirect
hook to redirect the term page to the single post when $wp_query->found_posts
returns 1
Just a note before I start. You should not create your own static links, they can cause issues when your permalink structure changes. Let get_term_link()
handle the link building. In your context, you can simply pass the complete term object to get_term_link()
. This does not cause extra db call, and you have a reliable link to the term page
NOTE: All code is untested
OPTION 1
You can try the following in your shortcode
add_shortcode( 'evalspecialties', 'doc_categories' );
function doc_categories()
{
$tax = 'team_group'; // slug of taxonomy to list
$specials = ''; //Define $specials as a empy string
// Get all the terms belonging to the team_group taxonomy
$terms = get_terms( $tax );
// IMPORTANT, make sure we have terms and also if we do not have a WP_Error
if ( !$terms
|| is_wp_error( $terms )
)
return $specials;
// Ok, everything checks out, lets continue
$specials .= '<div>';
foreach ( $terms as $term ) {
// Just in case we somehow set hide_empty to false through a filter
if ( 0 == $term->count )
continue;
$description = $term->description;
$term_link = get_term_link( $term );
// If we have more than one post, show term link, otherwise show post permalink
if ( 1 == $term->count ) {
$args = [
'posts_per_page' => 1,
'fields' => 'ids', //Just get postID
'tax_query' => [
[
'taxonomy' => $term->taxonomy,
'terms' => $term->term_id,
]
],
// Add any extra parameter
];
$q = get_posts( $args );
$term_link = get_permalink( $q[0] );
}
$link = '<a href="' . esc_url( $term_link ) . '"><h5>' . $term->name . '</h5></a>';
$imglink = '<a href="' . esc_url( $term_link ) . '"><img src="' . z_taxonomy_image_url($term->term_id) . '"></a>';
$specials .= '<div class="flex_column av_one_third specialty flex_column_div">';
$specials .= $link;
$specials .= $imglink;
$specials .= '</div>';
} //endforeach
$specials .= '</div>';
return $specials;
}
OPTION 2
This is the better cleaner way to do it. Here you basically just chect the value of $found_posts
, then redirect to the single post page if the value is 1. We will use template_redirect
here.
First quickly modify your shortcode
add_shortcode( 'evalspecialties', 'doc_categories' );
function doc_categories()
{
$tax = 'team_group'; // slug of taxonomy to list
$specials = ''; // Define $specials as a empy string
// Get all the terms belonging to the team_group taxonomy
$terms = get_terms( $tax );
// IMPORTANT, make sure we have terms and also if we do not have a WP_Error
if ( !$terms
|| is_wp_error( $terms )
)
return $specials;
// Ok, everything checks out, lets continue
$specials .= '<div>';
foreach ( $terms as $term ) {
// Just in case we somehow set hide_empty to false through a filter
if ( 0 == $term->count )
continue;
$description = $term->description;
$term_link = get_term_link( $term );
$link = '<a href="' . esc_url( $term_link ) . '"><h5>' . $term->name . '</h5></a>';
$imglink = '<a href="' . esc_url( $term_link ) . '"><img src="' . z_taxonomy_image_url($term->term_id) . '"></a>';
$specials .= '<div class="flex_column av_one_third specialty flex_column_div">';
$specials .= $link;
$specials .= $imglink;
$specials .= '</div>';
} //endforeach
$specials .= '</div>';
return $specials;
}
This will be the redirect action
add_action( 'template_redirect', function ()
{
global $wp_query;
// Check if found_posts is 1, if not, bail
if ( 1 != $wp_query->found_posts )
return;
// Only target or taxonomy page
if ( !is_tax( 'team_group' ) )
return;
// We only have one post, redirect to the single post page
$single_post = $wp_query->post;
$permalink = get_permalink( $single_post );
wp_redirect( esc_url( $permalink ) );
exit;
});
EDIT
The code above is now tested and working