2

I have a custom post type with several taxonomies within it

Eg. the post type is called campsites and the taxonomies are various counties: cheshire, staffordshire etc.

What I want to do is: list all of the counties and when you click on a county it links through to a page which lists all of the posts within that County. How can I do that.

1 Answer 1

3

There are plenty of functions for grabbing the data you need, a quick look at the Codex function reference reveals many useful functions you can use to build this.

Use get_term_link to get the link to that terms archive, and wp_get_object_terms to get the terms of an object/post.

To get all terms in a taxonomy use get_terms

You'll find examples on each function page, e.g. displaying all terms in a taxonomy and linking to them:

$args = array( 'taxonomy' => 'my_term' );

$terms = get_terms('my_term', $args);

$count = count($terms); $i=0;
if ($count > 0) {
    $cape_list = '<p class="my_term-archive">';
    foreach ($terms as $term) {
        $i++;
        $term_list .= '<a href="/term-base/' . $term->slug . '" title="' . sprintf(__('View all post filed under %s', 'my_localization_domain'), $term->name) . '">' . $term->name . '</a>';
        if ($count != $i) $term_list .= ' &middot; '; else $term_list .= '</p>';
    }
    echo $term_list;
}

Though that example could be improved greatly by replacing the hyperlink code with code that uses get_term_link.

There are other useful functions such as wp_list_categories but that function is not as flexible and limits you to a dropdown box or list rather than whatever arbitrary markup you'd prefer

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.