0

my site has 3 custom post types that each post type has a unique custom taxonomy to that post type.

I want to display these taxonomies in my single posts page and just the taxonomy related to that post type and not the others and without categories and tags.

setting the _builtin to false would do the trick for categories and tags but I can't figure out the way to display those custom taxonomies properly and my posts page show all custom taxonomies from all post types.

I don't know php perfectly and tried to find the code from multiple sites including this site and this is my code so far:

// get taxonomies terms links
function custom_taxonomies_terms_links() {
    global $post, $post_id;
    // get post by post id
    $post = &get_post($post->ID);
    // get post type by post
    $post_type = $post->post_type;
    // get post type taxonomies
    $args = array(
        'public'   => true,
        '_builtin' => false
    ); 

    $output = 'names'; // or objects
    $operator = 'and'; // 'and' or 'or'
    $taxonomies = get_taxonomies( $args, $output, $operator );

    $out = "<ul>";

    foreach ($taxonomies as $taxonomy) {        
        $out .= "<li>".$taxonomy.": ";
        // get the terms related to post
        $terms = get_the_terms( $post->ID, $taxonomy );

        if ( !empty( $terms ) ) {
            foreach ( $terms as $term )
                $out .= '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a> ';
        }

        $out .= "</li>";
    }

    $out .= "</ul>";

    return $out;
} 

any help will be appreciated.tnx

1

1 Answer 1

0

Try Below Code

function custom_taxonomies_terms_links() 
{
    global $post, $postid;

    $custom_taxonomies = get_post_taxonomies( $post );

    if($custom_taxonomies)
    {
        foreach ($custom_taxonomies as $custom_taxonomy) 
        {
              // get post type taxonomies
            $args = array(
                'public'   => true,
                '_builtin' => false
            ); 
            $args['name']=$custom_taxonomy;

            $output = 'names'; // or objects
            $operator = 'and'; // 'and' or 'or'
            $taxonomies = get_taxonomies( $args, $output, $operator );

            $out = "<ul>";

            foreach ($taxonomies as $taxonomy) 
            {        
                    $out .= "<li>".$taxonomy.": ";
                    // get the terms related to post
                    $terms = get_the_terms( $post->ID, $taxonomy );

                    if ( !empty( $terms ) ) {
                        foreach ( $terms as $term )
                            $out .= '<a href="' .get_term_link($term->slug, $taxonomy) .'">'.$term->name.'</a> ';
                    }

                    $out .= "</li>";
            }

            $out .= "</ul>";

            echo $out;
        }
    }
}

you can modify HTML tag placement in above code as per your requirement. Hope this will help you!

0

Your Answer

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

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