Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating common "Single Tag Page" for different Post Types.
I'm using next code:

$loop = new WP_Query( array( 'post_type' => 'any', 'tag' => single_term_title( '', false ), 'posts_per_page' => 10 ) );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
  <div class="entry-content">
     <?php the_excerpt(); ?>
  </div><!-- .entry-content -->
<?php endwhile; // End the loop. ?> 

It works fine until there is no taxonomy terms conflict.
For example:
If I have Category & Tag names - "Videos", Category will have slug - "/videos" & Tag "/videos-2".

In case when slug isn't the same as taxonomy name above code does not working.
I need an alternative function for single_term_title(), something like "single_term_slug()".

Any ideas?

P.S. I was thinking about "get_term_by()" function, but I didn't had luck to adopt it to the code above.

Updated: I've posted my code below.

share|improve this question

3 Answers 3

There is a reference to the term object in $wp_query, and a shortcut function to grab this (providing you are running WordPress 3.1): get_queried_object().

So, to get the slug you would do echo get_queried_object()->slug;

If you are not running WordPress 3.1, you will have to call the get_queried_object() on the global $wp_query:

global $wp_query; echo $wp_query->get_queried_object()->slug;

share|improve this answer
    
Thank you Joe Hoyle. –  Andrew Apr 4 '11 at 18:04
up vote 1 down vote accepted

Thank you guys for such quick response. Much appreciated!

Here is code for "global" Tags page (displaying terms of default 'post_tag' taxonomy):

<?php
$term_slug = get_queried_object()->slug;
    if ( !$term_slug )
    return;
    else  
$loop = new WP_Query( array( 'post_type' => 'any', 'tag' => $term_slug, 'posts_per_page' => 10 ) );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
  <div class="entry-content">
    <?php the_excerpt(); ?>
  </div><!-- .entry-content -->
<?php endwhile; // End the loop. ?>  

And the next code example is for custom taxonomy query (displaying terms of custom taxonomy):

<?php 
//http://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters
$term_slug = get_queried_object()->slug;
        if ( !$term_slug )
        return;
        else
$args = array(
    'tax_query' => array(
                     array(
                       'taxonomy' => 'gallery_category',
                       'field' => 'slug',
                       'terms' => $term_slug,
                       'posts_per_page' => 10
                     )
                   )
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
  <div class="entry-content">
    <?php the_excerpt(); ?>
  </div><!-- .entry-content -->
<?php endwhile; // End the loop. ?> 
share|improve this answer
    
Is this your answer to your own question, or a follow up part of your question? –  MikeSchinkel Apr 5 '11 at 5:27
    
This seems to me like an answer in context of original question, dismissing the flag on it. –  Rarst Apr 5 '11 at 15:10
    
@MikeSchinkel - This is my answer. I've posted whole solution code. #1 is for displaying terms of default 'post_tag' taxonomy, which is used in custom post types. #2 is for displaying terms of custom taxonomy. –  Andrew Apr 6 '11 at 11:01

Here is a forked single_term_slug() function out of single_term_title()

function single_term_slug( $prefix = '', $display = true ) {
    $term = get_queried_object();
    if ( !$term )
    return;
    if ( is_category() )
        $term_slug = apply_filters( 'single_cat_slug', $term->slug );
    elseif ( is_tag() )
        $term_slug = apply_filters( 'single_tag_slug', $term->slug );
    elseif ( is_tax() )
        $term_slug = apply_filters( 'single_term_slug', $term->slug );
    else
        return;
    if ( empty( $term_slug ) )
        return;
    if ( $display )
        echo $prefix . $term_slug;
    else
        return $term_slug;
}
share|improve this answer
    
Thank you Bainternet. –  Andrew Apr 4 '11 at 18:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.