WordPress, a robust and flexible CMS, allows developers to create highly custom client solutions. One of the powerful features WordPress offers is the use of custom taxonomies and terms to organize content. In this article, we’ll explore how to output custom taxonomy terms in your WordPress templates, which is crucial when creating custom themes for your clients.
Understanding Taxonomies and Terms
Before diving into the code, it’s best to understand what taxonomies and terms are. Taxonomies in WordPress are used to group posts and custom post types together. They come in two forms: categories and tags, but you can also create your custom taxonomies. Terms are the items within a taxonomy; for instance, the categories are terms within the ‘category’ taxonomy.
Fetching and Outputting a Single Term
To fetch and output a single term associated with a post, we can use various WordPress functions. Here’s a snippet using the get_the_terms
function along with array_shift
to fetch the first term of a custom taxonomy called ‘markets’:
Using get_the_terms
:
// Assuming you have a WP_Query object $query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$terms = get_the_terms( get_the_ID(), 'markets' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term = array_shift( $terms ); // Taking the first term
$term_info = get_term( $term, 'markets' );
if ( ! is_wp_error( $term_info ) ) {
echo $term_info->name; // Output the term name
}
}
}
wp_reset_postdata(); // Restore original Post Data
}
In the above code snippet, we are fetching all terms associated with the current post in the ‘markets’ taxonomy using get_the_terms
, then using array_shift
to get the first term, and echo
to output the term name.
Below are code snippets demonstrating how to use wp_get_post_terms
functions to retrieve the current term
Using wp_get_post_terms
:
// Assuming you have a WP_Query object $query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$terms = wp_get_post_terms( get_the_ID(), 'markets', array('fields' => 'names') );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
echo $terms[0]; // Output the name of the first term
}
}
wp_reset_postdata(); // Restore original Post Data
}
Listing Multiple Terms
When a post is associated with multiple terms, you might want to list all of them. Below is a code snippet using get_the_terms
and implode
to fetch and output all terms associated with a post in a comma-separated list:
Using wp_get_post_terms
:
// Assuming you have a WP_Query object $query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$terms = wp_get_post_terms( get_the_ID(), 'markets', array('fields' => 'names') );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
$term_list = implode( ', ', $terms ); // Join term names into a comma-separated string
echo $term_list;
}
}
wp_reset_postdata(); // Restore original Post Data
}
In this snippet, get_the_terms
fetches all the terms and implode
joins them into a comma-separated string, which is then output using echo
.
Using get_the_terms
:
// Assuming you have a WP_Query object $query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$terms = get_the_terms( get_the_ID(), 'markets' );
if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) {
$term_names = array_map( function( $term ) {
return $term->name;
}, $terms );
$term_list = implode( ', ', $term_names ); // Join term names into a comma-separated string
echo $term_list;
}
}
wp_reset_postdata(); // Restore original Post Data
}
Linking Terms to Taxonomy Pages
Creating links to the taxonomy pages of terms can provide a better navigational experience for users. Below are snippets to output one term and multiple terms with links to their taxonomy pages:
Outputting One Term:
// Assuming you have a WP_Query object $query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$terms = get_the_terms( get_the_ID(), 'markets' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term = array_shift( $terms ); // Taking the first term
$term_link = get_term_link( $term );
if ( ! is_wp_error( $term_link ) ) {
echo "<a href='{$term_link}'>{$term->name}</a>";
}
}
}
wp_reset_postdata(); // Restore original Post Data
}
Outputting Multiple Terms:
// Assuming you have a WP_Query object $query
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$terms = get_the_terms( get_the_ID(), 'markets' );
if ( $terms && ! is_wp_error( $terms ) ) {
$term_links = array();
foreach ( $terms as $term ) {
$term_link = get_term_link( $term );
if ( ! is_wp_error( $term_link ) ) {
$term_links[] = "<a href='{$term_link}'>{$term->name}</a>";
}
}
$term_list = implode( ', ', $term_links ); // Join term links into a comma-separated string
echo $term_list;
}
}
wp_reset_postdata(); // Restore original Post Data
}
Conclusion
As a WordPress developer, it is essential to know how to output custom taxonomy terms in your templates. This skill is particularly important when creating custom themes for clients. The following code snippets demonstrate various methods to fetch and output terms, both singularly and in lists. Additionally, you can create links to taxonomy pages for better navigation. By mastering these techniques, you can deliver more organized and navigable websites to your clients, showcasing your expertise in custom WordPress development.
Hire an Orlando WordPress Web Designer and Elevate Your Online Presence
While understanding the seven principles of website design is essential, sometimes, entrusting the creation of your online presence to a professional is the key to achieving remarkable results. In Orlando, a city known for its vibrant business landscape and thriving digital economy, the role of an Orlando web designer in your company’s success cannot be overstated.
The Web Design Expert in Orlando
An Orlando web designer, well-versed in web design principles, brings a wealth of expertise. They don’t just craft websites; they craft experiences that resonate with your target audience.
Here’s what I can do better than other freelance web designers in Orlando:
Accessible Websites for Inclusivity and Best Practices
Web accessibility is of utmost importance as it ensures that your website is easily usable by everyone, including those with disabilities. By implementing accessible design elements such as alt text for images, keyboard navigation, and proper heading structures, an Orlando web designer can ensure that the website is easily accessible and usable by all. This makes your website more accessible and expands your potential audience, while also helping you align with best practices and legal requirements.
Responsive Design for All Devices
Mobile devices are extensively used, and having a responsive website design has become imperative. Orlando web designers specialize in developing websites that can adapt seamlessly to different devices, screen sizes, and resolutions. A responsive website ensures that your content will appear and function smoothly, irrespective of whether your audience is using a desktop computer, tablet, or smartphone.
Strategic Approach for Business Growth
It’s important to acknowledge that every business has its own unique qualities, and this is something that a web designer fully understands. They collaborate closely with you to create a personalized, strategic plan that aligns with your company’s goals. Whether it’s designing captivating landing pages or improving user experiences, a skilled web designer in Orlando will ensure that your website is not just visually appealing but also a valuable asset for generating leads.
Why hire a Web Designer locally?
I’m located in Orlando, Florida, and I understand the surrounding areas and what’s happening nearby. When working with a web designer, they must understand your audience and where your business sells.
While understanding the seven principles of design is invaluable, leveraging the expertise of an Orlando web designer can take your online presence to the next level. By partnering with a professional who incorporates these principles into every aspect of your website, you’re not just building a digital storefront but crafting an immersive, accessible, and strategically sound online experience.
If you’re near Orlando and seeking to enhance your company’s digital footprint, consider collaborating with an Orlando web design specialist. Together, you can create a website that will bring your business new opportunities and success.