In general to get the posts of your custom post type you need to query post_type, if you want only the categories you could just pull the categories instead of the entire post type.
<?php query_posts(array( 'post_type' => 'CustomPostType' )); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_category(', '); ?>
<?php endwhile; else: ?>
<p>Sorry, no matches found.</p>
<?php endif; ?>
Or you could use get_terms()?
Quick example:
$terms = get_terms('CustomPostType');
foreach ( $terms as $term ) {
echo $term->name.'<br />';
}
Are you listing anything else on that archive page? If you are for example listing posts (or other information) from the same post type you could combine it.
EDIT:
I apologize I read over the comments in the script and thought you solely needed help with the rest. To just get the term id
you could use get_terms()
(as referred above). And then take a look at the following parameters:
orderby (string)
id
count
name - Default
slug
term_group
none
order (string)
ASC - Default
DESC
number (integer)
The maximum number of terms to return. Default is to return them all.
fields (string)
all - returns an array of term objects - Default
ids - returns an array of integers
names - returns an array of strings
count - (3.2+) returns the number of terms found
id=>parent - returns an associative array where the key is the term id and the value is the parent term id if present or 0
Not including any other parameters it would look something like this:
$CustomPostType_ids = get_terms( 'CustomPostType', array(
'orderby' => 'id',
'order' => 'ASC',
'number' => 1,
'fields' => 'ids'
) );
You could also return all values and then just pull term_id
.