• Resolved lukeshumard

    (@lukeshumard)


    Hello,

    I’m attempting to create a hyperlink within a post template that uses a slug from a specific taxonomy (“venue”). This is how the hyperlink should look for each post…
    http://my-site.com/venues/{taxonomy-slug}/

    There is only one value for each “venue” taxonomy. The rest of the link is static, so all that needs to happen is have the “venue” slug added to the end of the post. Here’s the code I have so far…

    <a href="<?php echo get_option('home'); ?>/venues/
    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'venue' ) );
    $interestname = $term->name; echo $interestname; ?>">
    Venue Name</a>

    This is a single line of code, I’ve just used breaks to make it more readable and so scrolling isn’t necessary when reading it on this forum.

    I know someone will be quick to point out that it’s probably a lot easier to use something else besides a taxonomy for this, I actually do need to and this is just a small part of the project. This has to be done with the taxonomy.

    Any ideas or help is much appreciated. Thank you.

Viewing 15 replies - 1 through 15 (of 17 total)
  • try to add a print_r($term) to your code to see what $term contains.

    does $interestname come out empty?

    Thread Starter lukeshumard

    (@lukeshumard)

    It’s just blank, even when I add print_r$(term).

    just throwing questions around:

    http://codex.wordpress.org/Function_Reference/get_term_by

    if print_r($term); does not show any values, then there is probably something wrong with the parameters of get_term_by():

    what does for instance echo get_query_var('term'); and echo get_query_var( 'venue' ); show?

    is there a reason why you use get_query_var('term') and the same vor ‘venue’; instead of directly using the strings?

    Thread Starter lukeshumard

    (@lukeshumard)

    I was using them in an earlier template (taxonomy archive for a different field, “interests”) when getting a title in the template. I stripped the get_query_var() and used the strings directly, but it still showed blank. I’m not against using some other means of retrieving the data besides get_term_by(), but I don’t know what my alternatives would be.

    I am also looking for a solution to this problem. Hopefully someone can help out soon. Thanks

    @lukeshumard:

    this would get all terms for the ‘venue’ taxonomy;
    $term = get_terms('venue');

    if you only have one term in ‘venue, you should get the slug of it with this:
    $interestname = $term[0]->slug;

    so your code should hopefully work this way:

    <a href="<?php echo get_option('home'); ?>/venues/
    <?php $term = get_terms( 'venue' );
    $interestname = $term[0]->slug; echo $interestname; ?>">
    Venue Name</a>

    @adam533
    if you have exactly the same challenge, this might also help you.
    if your problem is different, you may consider to start a new thread and formulate your question in more detail.

    Thread Starter lukeshumard

    (@lukeshumard)

    @alchymyth
    I actually tried that a few days ago and it worked…sort of. the output was a venue, but not the right venue that this post was tagged with in the venue taxonomy. Using <?php $term = get_terms( 'venue' );, I then used print_r($term); to see what my output would be, and it was (sure enough) an array of all the values I had input for “venues.” When I was using $interestname = $term[0]->slug; echo $interestname;, it was echoing the slug value for the first term in the array (obviously).

    I think I’m really close now, but I just need to find someway to identify which term_id the post is tagged with in the specific taxonomy.

    What’s the output for the following?

    echo get_query_var( 'term' );

    ..and also..

    echo get_query_var( 'venue' );

    ..do either of them come up empty?

    Thread Starter lukeshumard

    (@lukeshumard)

    Both of them come up empty. Here’s what’s printed for $term = get_terms( 'venue' ); $interestname = $term[0]->slug; echo $interestname;

    Array ( [0] => stdClass Object ( [term_id] => 9 [name] => Venue Name  [slug] => venue-name [term_group] => 0 [term_taxonomy_id] => 11 [taxonomy] => venue [description] => [parent] => 0 [count] => 1 )

    …and this array goes on for the 9 different values I’ve inputted into the “venue” taxonomy. I just need to find a way to figure out what term id in the array is the one I’ve tagged the post with.

    How are you registering your taxonomy?

    Also, have you tried querying your site like so..

    example.com/?taxonomy=yourtaxname&term=yourtermname

    As long as the taxonomy is registered with the query var set to true, or set with the label above the URLs should work, and i think as you say, all you need is a template (or page) that lists each term, linking to that given taxonomy’s various term pages. A page template seems like the obvious choice..

    get_term_link()

    ..should help build the term links, all you need now is a basic page template that lists terms of a taxonomy.

    As far as URLs such as..

    example/taxonomy/term/

    ..go, that should already work if the taxonomy was registered with the rewrite parameter set to true, else you’ll only have the regular query string method available as i’ve shown further up.. ie ?taxonomy=taxname etc..

    Also note, all you need to display a terms posts is provide your theme with a taxonomy template(taxonomy.php), like you would for categories or tags, and in the most basic form this is all you need in that file.
    http://codex.wordpress.org/The_Loop_in_Action#The_World.27s_Simplest_Index_Page

    Thread Starter lukeshumard

    (@lukeshumard)

    I’m not trying to create unique pages for these taxonomy values, or link to their respective pages, but to use their slug as a part of a link to a different post within a different category that has the same slug name. Because of that, using get_term_link doesn’t work, and the taxonomy.php template is used for displaying something else totally different.

    Here’s how I’m registering my taxonomies…

    add_action( 'init', 'create_my_taxonomies', 0 );
    
    function create_my_taxonomies() {
    	register_taxonomy( 'venue', 'post', array( 'hierarchical' => false, 'label' => 'Venue', 'query_var' => true, 'rewrite' => array( 'slug' => 'venue' ) ) );
    	register_taxonomy( 'interest', 'post', array( 'hierarchical' => false, 'label' => 'Interest', 'query_var' => true, 'rewrite' => array( 'slug' => 'interest' ) ) );
    };

    Ok, so is the format of the link in your opening post correct?

    Do you want a list of links in that format using all the terms that exist under a given taxonomy?

    I’m not sure i follow… please clarify if possible.

    Thread Starter lukeshumard

    (@lukeshumard)

    The format of the link is in the opening post, that’s correct.

    What I want is on an individual post template. I want the slug of whatever value that post has been tagged with to be displayed in the link. There’s only one value tagged in this taxonomy per post, so there’s no need to worry about lists.

    For example, if this post has a “venue taxonomy value of “Gallery Fun”, then I want the link to be http://www.example.com/venues/gallery-fun. Also, the taxonomy name is “venue” not “venues”, so using get_term_link won’t work, nor would something linking to the actual taxonomy.

    Is that clear? I know it’s a bit confusing and also not the typical use of a taxonomy, but it’s something I need to have done.

    Ok i think i get what it is you’re trying to do now..

    get_terms()

    What you had before, is a generic “fetch all terms” with the provided args type function, what you need is a “get the posts terms” type function, such as get_the_terms() or wp_get_post_terms(), so the terms received are specific to the given post.

    wp_get_post_terms()
    http://codex.wordpress.org/Function_Reference/wp_get_post_terms
    get_the_terms()
    http://core.trac.wordpress.org/browser/tags/2.9.2/wp-includes/category-template.php#L854

    Both accept the post ID as the first parameter.

    Thread Starter lukeshumard

    (@lukeshumard)

    Exactly what I needed. Thanks! Here is the final code…

    <?php $eventpostid = $post->ID;
    $eventslug = wp_get_post_terms( $eventpostid, 'venue' );
    $eventvenueslug = $eventslug[0]->slug;
    $eventvenuetitle = $eventslug[0]->name; ?>
    <a href="<?php echo get_option('home'); ?>/venues/<?php echo $eventvenueslug; ?>/"><?php echo $eventvenuetitle; ?></a>

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘Retrieve Taxonomy slug(s)’ is closed to new replies.