WordPress.org

Forums

WordPress listing custom post type categories and tags, really stuck (1 post)

  1. matt3224
    Member
    Posted 1 year ago #

    I have a custom post type and i need to list the categories and tags with custom markup around then so that each one is an option in a select element.

    The custom post type itself works without issue but no matter what i try i cannot get a working function that lists the categories and tags in the way i want.

    P.S. I need the categories in one select option drop down and the tags in a separate drop down. Thanks.

    /* --- RESOURCE POST TYPE --- */
    
    add_action( 'init', 'register_cpt_resources' );
    
    function register_cpt_resources() {
    
        $labels = array(
            'name' => _x( 'Resources', 'resource' ),
            'singular_name' => _x( 'Resource', 'resource' ),
            'add_new' => _x( 'Add New', 'resource' ),
            'add_new_item' => _x( 'Add New Resource', 'resource' ),
            'edit_item' => _x( 'Edit Resource', 'resource' ),
            'new_item' => _x( 'New Resource', 'resource' ),
            'view_item' => _x( 'View Resource', 'resource' ),
            'search_items' => _x( 'Search Resources', 'resource' ),
            'not_found' => _x( 'No resources found', 'resource' ),
            'not_found_in_trash' => _x( 'No resources found in Trash', 'resource' ),
            'parent_item_colon' => _x( 'Parent Resource:', 'resource' ),
            'menu_name' => _x( 'Resources', 'resource' )
        );
    
        $args = array(
            'labels' => $labels,
            'hierarchical' => false,
            'description' => 'Resource Hub Items',
            'supports' => array( 'title', 'editor', 'revisions' ),
            'taxonomies' => array( 'category', 'post_tag' ),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'show_in_nav_menus' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => true,
            'query_var' => true,
            'can_export' => true,
            'rewrite' => true,
            'capability_type' => 'post',
            'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail' ),
            'menu_icon' => 'dashicons-welcome-learn-more'
        );
    
        register_post_type( 'resource', $args );
    }

    And here's the code I've tried:

    <?php
    $terms = get_the_terms( $post->ID , 'resource' );
    foreach ( $terms as $term ) { ?>
        <option class=" <?php echo $term->name; ?>"><?php echo $term->name; ?></option>
    <?php } ?>

    I have also tried the following which gets the info but its not in the markup i need:

    <?php
    $customPostTaxonomies = get_object_taxonomies('resource');
    
    if(count($customPostTaxonomies) > 0)
    {
         foreach($customPostTaxonomies as $tax)
         {
             $args = array(
                  'orderby' => 'name',
                  'show_count' => 0,
                  'pad_counts' => 0,
                  'hierarchical' => 1,
                  'taxonomy' => $tax,
                  'title_li' => ''
                );
    
             wp_list_categories( $args );
         }
    }
    ?>

    Any help would be really appreciated

Topic Closed

This topic has been closed to new replies.

About this Topic