Creating posts and pages in WordPress is sufficient in most cases, but there are situations when you may need to store and display custom content.
WordPress supports registering custom post types, custom taxonomies and storing custom meta-data for each type of post.
Displaying custom fields is relatively simple through the use of get_post_meta function. But how can you display custom taxonomy terms attached to the current custom post type?
First of all add the following helper function to your functions.php
file.
The eo_get_tax_terms
function must be called within the loop. The first parameter is either a string (representing a taxonomy slug) or an array of taxonomy slugs (registered to the the current post type). You can specify a limit on the number of terms returned by the function (enter -1
to return them all), whether to return links to term archive pages as well as the separator character between taxonomy terms.
Depending on where you want to display the taxonomy terms and the custom taxonomies you have defined, add code such as the following to functions.php
.
The above code fragment assumes there are 4 custom taxonomies defined (with slugs ‘taxonomy{x}’, x = 1..4). The function displays the terms in various ways, so change the code as you see it fits.
Note that you may use built-in WordPress taxonomies as well, namely categories and tags, whose slugs are category
and post_tag
respectively, if these have been registered with your custom post type.