Grow your CSS skills. Land your dream job.

I need to get the category ID for a custom taxonomy

  • # February 7, 2013 at 1:38 am

    I have a custom taxonomy called stores, so I can’t use the code for a standard category as defined here http://codex.wordpress.org/Function_Reference/get_the_category

    So I took a look at http://codex.wordpress.org/Function_Reference/get_term_by but can’t figure what I need to do.

    This is my code

    < ?php $cat_id = 47;
    $value = get_field(‘extended_store_descriptions’, ‘stores_’ . $cat_id );
    echo $value;
    ?>

    All I need to do is make that number 47 dynamic instead of static and I’ve tried this:

    < ?php
    $cat_id = get_query_var(‘stores’);
    $value = get_field(‘extended_store_descriptions’, ‘stores_’ . $cat_id );
    echo $value;

    ?>

    But that didn’t work. I don’t know if I should be using this:

    [http://codex.wordpress.org/Function_Reference/wp_get_post_terms](http://codex.wordpress.org/Function_Reference/wp_get_post_terms “”)

    or this

    [http://codex.wordpress.org/Function_Reference/get_terms](http://codex.wordpress.org/Function_Reference/get_terms “”)

    Thanks so much!

    # February 7, 2013 at 9:02 pm

    Hopefully some kind soul with the chops can help me out:

    When I do this:

    < ?php
    $terms = get_the_terms($post->ID, ‘stores’);

    print_r($terms);
    ?>

    It gets me close to what I want:

    This is outputted:

    _Array ( [47] => stdClass Object ( [term_id] => 47 [name] => Amazon.com [slug] => amazon-com [term_group] => 0 [term_taxonomy_id] => 49 [taxonomy] => stores [description] =>
    Amazon Coupon Codes
    Amazon is the world’s largest online retailer. The company also produces consumer electronics, and is a major provider of cloud computing services. [parent] => 0 [count] => 1 [object_id] => 40 ) )_

    But I only want the number 47 in there

    # February 7, 2013 at 9:03 pm

    $terms

    # February 7, 2013 at 9:43 pm

    I figured it out with some help from this page: [http://wordpress.org/support/topic/trying-to-output-a-posts-terms-taxonomy-as-text-not-urls#post-1188801](http://wordpress.org/support/topic/trying-to-output-a-posts-terms-taxonomy-as-text-not-urls#post-1188801 “”)

    < ?php
    // Get terms for post
    $terms = get_the_terms( $post->ID , ‘stores’ );

    // Loop over each item since it’s an array
    foreach( $terms as $term ) {
    // Print the term_id method from $term which is an OBJECT
    // $lovers will be grabs the current store page id
    $lovers= $term->term_id;
    // Get rid of the other data stored in the object, since it’s not needed
    unset($term);
    }
    ?>

    < ?php
    $cat_id = $lovers;
    $value = get_field(‘extended_store_descriptions’, ‘stores_’ . $cat_id );
    echo $value;

    ?>

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".