Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Display custom taxonomy (like “the_category”) for custom post type

  • This topic is empty.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #35377
    jonedwards
    Participant

    Hi there

    I have a custom post type, “entrepreneurs”, with a custom taxonomy, “sectors” attached to it. Here is the code (http://codepad.org/ceQH4a6a) for my custom post type registrations.

    Things you might need to know:

    • “Entrepreneurs” are heirachical, like pages.
    • “Sectors” are like the categories for the post type

    What I want to do…

    In both my archive and single views, I would like to display the assigned sector name for the post below the title – in the same way that you would in, say, a blog article, using “the_category” or something similar. Like so:

    <?php the_category('', '', $post_id) ?>

    The full block of code can be found here (http://codepad.org/ub3c9N9P).

    I want the H4 to output the custom taxonomy, without a link – resulting in the following layout (http://db.tt/xiL3w0ty). The grey text below the title is sector taxonomy.

    I’ve tried a number of solutions found on various forums and resources, but none are working for me. Any help would be appreciated.

    Thanks in advance.

    #91492
    jonedwards
    Participant

    Thanks for the nudge in the right direction, Rob.

    I used the following:

    <h4><?php $terms = get_terms("sectors"); $count = count($terms); if ( $count > 0 ){ foreach ( $terms as $term ) { echo $term->name; } } ?> Sector</h4>

    It seems to work.

    As this is a “client” website, and they usually screw everything up when editing, I’d prefer to be able to limit the number of terms outputted to 1. Any help..?

    #91497
    Brightonmike
    Member

    I use the following to display a term without a link. Not sure how to limit but should be easy enough.

    AFAIK, this is the correct code to use. Yours looks kinda bloated.

    
    $terms = get_the_terms( $post->ID , 'taxonomyname' );
    foreach ( $terms as $term ) {
    echo $term->name;
    }
    ?>
    #91498
    jonedwards
    Participant

    Hey Mike,

    Thanks for sharing that. Always good to trim the fat… It worked.

    Cheers.

    #136511
    treysea
    Member

    Is it possible to modify this so that the term that is output is wrapped in a link that would show the archive of that taxonomy term? (the way a category often is shown)

    #149576
    Kennedy Otis
    Participant

    Hi guys.

    Am a beginner to wordpress and I i have been playing around with custom post types and taxonomies. I have product custom post type and three taxonomies assigned to that post type. Here is my functions file for http://codepad.org/opDNhZPf . so the product should have a category, brand and the type. Then I would like to have a query which will return a products with their respective taxonomies. I would really appreciate if i can get help.

    #156888
    Duncan
    Participant

    I have played about with the code some more as I needed to display only one custom taxonomy that was a link.
    However I am unable to get the link to work – anyone have any ideas on this?

    See here:

    <?php $terms = get_the_terms( $post->ID , 'yourcustomtaxonomyhere' ); 
                        foreach ( $terms as $term ) {
                        echo '<a href="' . $term_link . '">' . $term->name . '</a>';
                        } 
                    ?>
    
    #156891
    Duncan
    Participant

    CRACKED IT!

    I have managed to get the code working, see following:

    <?php $terms = get_the_terms( $post->ID , 'yourtaxonomyhere' ); 
                        foreach ( $terms as $term ) {
                            $term_link = get_term_link( $term, 'yourtaxonomyhere' );
                            if( is_wp_error( $term_link ) )
                            continue;
                        echo '<a href="' . $term_link . '">' . $term->name . '</a>';
                        } 
                    ?>
    
    #160065
    mattrock
    Participant

    Thanks @Brightonmike. Your code snippet is exactly what I was looking for. Cheers!

    #254831
    nicoleamanda
    Participant

    Hey guys! Semi new to custom taxonomy but this code isn’t working for me! :( What am I doing wrong? http://nicoleamanda.ca/blog/test

    #264158
    alexanderbiscajin
    Participant

    You can create a custom taxonomy using the code in functions.php

    `

    add_action( ‘init’, ‘create_cw_hierarchical_taxonomy’, 0 );
    //create a custom taxonomy name
    function create_cw_hierarchical_taxonomy() {
    $labels = array(
    ‘name’ => _x( ‘Topics’, ‘taxonomy general name’ ),
    ‘singular_name’ => _x( ‘Topic’, ‘taxonomy singular name’ ),
    ‘search_items’ => __( ‘Search Topics’ ),
    ‘all_items’ => __( ‘All Topics’ ),
    ‘parent_item’ => __( ‘Parent Topic’ ),
    ‘parent_item_colon’ => __( ‘Parent Topic:’ ),
    ‘edit_item’ => __( ‘Edit Topic’ ),
    ‘update_item’ => __( ‘Update Topic’ ),
    ‘add_new_item’ => __( ‘Add New Topic’ ),
    ‘new_item_name’ => __( ‘New Topic Name’ ),
    ‘menu_name’ => __( ‘Topics’ ),
    );
    // taxonomy register
    register_taxonomy(‘topics’,array(‘post’), array(
    ‘hierarchical’ => true,
    ‘labels’ => $labels,
    ‘show_ui’ => true,
    ‘show_admin_column’ => true,
    ‘query_var’ => true,
    ‘rewrite’ => array( ‘slug’ => ‘topic’ ),
    ));
    }

    `

    Reference : https://www.wpblog.com/create-custom-taxonomies-in-wordpress/

    #297550
    assetweb
    Participant

    Thanks. This worked best for me with ACF v5.8.0 showing only terms for a specific custom post.
    $terms = get_the_terms($post->ID, 'discipline');

    #297761
    Aleaume20
    Participant

    add_action( ‘init’, ‘create_cw_hierarchical_taxonomy’, 0 );
    //create a custom taxonomy name
    function create_cw_hierarchical_taxonomy() {
    $labels = array(http://www.rachat-de-credit-simulation.com/rachat-de-credit
    ‘name’ => _x( ‘Topics’, ‘taxonomy general name’ ),
    ‘singular_name’ => _x( ‘Topic’, ‘taxonomy singular name’ ),
    ‘search_items’ => __( ‘Search Topics’ ),
    ‘all_items’ => __( ‘All Topics’ ),
    ‘parent_item’ => __( ‘Parent Topic’ ),
    ‘parent_item_colon’ => __( ‘Parent Topic:’ ),
    ‘edit_item’ => __( ‘Edit Topic’ ),
    ‘update_item’ => __( ‘Update Topic’ ),
    ‘add_new_item’ => __( ‘Add New Topic’ ),
    ‘new_item_name’ => __( ‘New Topic Name’ ),
    ‘menu_name’ => __( ‘Topics’ ),
    );
    // taxonomy register
    register_taxonomy(‘topics’,array(‘post’), array(
    ‘hierarchical’ => true,
    ‘labels’ => $labels,
    ‘show_ui’ => true,
    ‘show_admin_column’ => true,
    ‘query_var’ => true,
    ‘rewrite’ => array( ‘slug’ => ‘topic’ ),
    ));
    }

    `

    Reference : https://www.wpblog.com/create-custom-taxonomies-in-wordpress/

Viewing 13 posts - 1 through 13 (of 13 total)
  • The topic ‘Display custom taxonomy (like “the_category”) for custom post type’ is closed to new replies.