Forums

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

Home Forums Back End I need to get the category ID for a custom taxonomy

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #42540
    sanclementejoe
    Participant

    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

    $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:

    $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!

    #123752
    sanclementejoe
    Participant

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

    When I do this:

    $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

    #123753
    TylerNYC
    Member

    $terms

    #123756
    sanclementejoe
    Participant

    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 “”)

    // 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);
    }
    ?>

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

    ?>

Viewing 4 posts - 1 through 4 (of 4 total)
  • The forum ‘Back End’ is closed to new topics and replies.