WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Echo Taxonomy Slug (8 posts)

  1. Ian
    Member
    Posted 4 years ago #

    I'm trying to use the taxonomy slug in some code and I'm just not sure how to do it. I feel like I'm close and I'm not sure what I'm missing - sorry if this is something really obvious - I've just been searching and can't find it.

    <?php
    if ( is_taxonomy($taxonomy) ) {
    $yourtax = get_taxonomy ($taxonomy);
    echo '
      <OBJECT data="http://www.tradereadingorder.com/taxonomy-image/' . $yourtax->slug . '.jpg" type="image/jpg"><!-- Else, try the 404 image -->
        <OBJECT data="http://www.tradereadingorder.com/404.jpg" type="image/jpg">
        </OBJECT> </OBJECT>';
    }
    ?>
  2. MichaelH
    Member
    Posted 4 years ago #

    Not sure what you are wanting but after this line $yourtax = get_taxonomy ($taxonomy); put this to help see the proper value you want echo "<pre>"; print_r($yourtax); echo "</pre>";

    If you are looking for a particular slug for a particular term in a given taxonomy, you might want to look at get_term_by.

    If you are trying to do this on a taxonomy archive page you might want to look at is_tax().

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

    I believe what you're after is..

    $yourtax->name

    or

    $yourtax->label

    Slug isn't in the object you get back from get_taxonomy.

  4. Ian
    Member
    Posted 4 years ago #

    close! in fact, yourtax->label helped me solve the folder naming problem I was having. but I still need to be able to display the term.. this is what I've got so far (without trying michael's stuff yet)

    <?php
    if ( is_taxonomy($taxonomy) ) {
    $yourtax = get_taxonomy ($taxonomy);
    echo '
      <OBJECT data="http://www.tradereadingorder.com/' . $yourtax->name . '-image/' . example-slug-here . '.jpg" type="image/jpg"><!-- Else, try the 404 image -->
        <OBJECT data="http://www.tradereadingorder.com/404.jpg" type="image/jpg">
        </OBJECT> </OBJECT>';
    }
    ?>
  5. Ian
    Member
    Posted 4 years ago #

    yeah this would be for at the start of my index, so if it's a taxonomy archive page, it displays an image named after the slug of that taxonomy.

    so am I supposed to use is_tax to get the slug instead of is_taxonomy?

  6. Ian
    Member
    Posted 4 years ago #

    I'm a little confused I guess. I'll start from the top - I'm trying to modify this code to work with my custom taxonomies, just as it's already working with my tags..

    <?php
    if ( is_tag() ) {
    $term_id = get_query_var('tag_id');
    $taxonomy = 'post_tag';
    $args ='include=' . $term_id;
    $terms = get_terms( $taxonomy, $args );
    echo '
      <OBJECT data="http://www.tradereadingorder.com/character-image/' . $terms[0]->slug . '.jpg" type="image/jpg"><!-- Else, try the 404 image -->
        <OBJECT data="http://www.tradereadingorder.com/404.jpg" type="image/jpg">
        </OBJECT> </OBJECT>';
    }
    ?>
  7. Ian
    Member
    Posted 4 years ago #

    aha! I think I figured it out. this is what I ended up doing:

    <?php
    if ( is_tax('creator') ) {
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    echo '
      <OBJECT data="http://www.tradereadingorder.com/creator-image/' . $term->slug . '.jpg" type="image/jpg"><!-- Else, try the 404 image -->
        <OBJECT data="http://www.tradereadingorder.com/404.jpg" type="image/jpg">
        </OBJECT> </OBJECT>';
    }
    ?>

    It seems to work. And also, the sections are separate for each kind of taxonomy now, which works for me, in case I ever want to further customize them!

  8. Mark / t31os
    Moderator
    Posted 4 years ago #

    You can follow the tag example, just change taxonomy to your taxonomy (Or use the var, i assume $taxonomy is being populated).

    The object you get back from get_taxonomy() does not contain any terms, it contains data on the taxonomy, not the terms that exist for that taxonomy, so take this line from your tags code.

    $terms = get_terms( $taxonomy, $args );

    If taxonomy in that case was something other than tags, you get the terms for that taxonomy instead.

    So for example, if i were to call..

    $terms = get_taxonomy( 'post_tag' );

    I'd not get any actual tags back, just data about the taxonomy(post_tag in this case)..

    post_tag is a taxonomy
    tags available are terms of that taxonomy

    I think perhaps you just have a little confusion in what a taxonomy is and what a term is, though your tag code looks fine, your taxonomy code should follow the same procedure, and only need a few minor changes, namingly to the taxonomy(which obviously will be something other than post_tag)..

    if( is_taxonomy( $taxonomy ) )

    ..should be fine for the condition..

    Print/Dump the query vars so you can see what you're working with..

    ?><pre><?php print_r( $wp_query->query_vars) ?></pre><?php

Topic Closed

This topic has been closed to new replies.

About this Topic