WordPress.org

Ready to get started?Download WordPress

Forums

Can't display custom post type category using get_the_terms (4 posts)

  1. khyams
    Member
    Posted 1 year ago #

    I've successfully created a custom post type of "World Tour" (worldtour), and have added a custom taxonomy called "Locations" to go with. I'm trying to use get_the_terms to display each posts' location, but am not having any luck.

    Here is my code to create the taxonomy:

    function my_taxonomies_worldtour() {
    	$labels = array(
    		'name'              => _x( 'Locations', 'taxonomy general name' ),
    		'singular_name'     => _x( 'Location', 'taxonomy singular name' ),
    		'search_items'      => __( 'Search Locations' ),
    		'all_items'         => __( 'All Locations' ),
    		'parent_item'       => __( 'Parent Location' ),
    		'parent_item_colon' => __( 'Parent Location:' ),
    		'edit_item'         => __( 'Edit Location' ),
    		'update_item'       => __( 'Update Location' ),
    		'add_new_item'      => __( 'Add New Location' ),
    		'new_item_name'     => __( 'New Location' ),
    		'menu_name'         => __( 'Locations' ),
    	);
    	$args = array(
    		'labels' => $labels,
    		'hierarchical' => true,
    		'slug' => 'location'
    	);
    	register_taxonomy( 'worldtour_category', 'worldtour', $args );
    }
    add_action( 'init', 'my_taxonomies_worldtour', 0 );

    And here is my string attempting to display the location:

    <?php get_the_terms( $post->ID, 'location' ); ?>

    I'm new to using a taxonomy with a custom post type, so if I am missing anything or if anything is incorrect then could you please let me know? If any other information is needed then I will try my best to supply it.

  2. hilukasz
    Member
    Posted 1 year ago #

    I am having a similar problem. Anyone find any solutions?

  3. nommin
    Member
    Posted 1 year ago #

    Perhaps...
    <?php get_the_terms( get_the_ID(), 'location' ); ?>

  4. nommin
    Member
    Posted 1 year ago #

    Or otherwise...

    <?php wp_get_object_terms( $post->ID, 'location' ); ?>

Topic Closed

This topic has been closed to new replies.

About this Topic