Support » Fixing WordPress » Automatic post title loop in taxonomy.php

  • Many thanks to Justin Tadlock for posting the following piece of code in the post Using custom taxonomies to create a movie database:

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );

    It helped me in writing the following bit of code (subject to review). The code will automatically show the posts for whatever custom taxonomy term page one is on. In order to have an automatic loop in taxonomy.php it appears that one might be able to do the following:

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>              
    
    <?php query_posts( array($term->taxonomy => $term->name) ); ?>
    
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    <?php the_title(); ?>
    
    <?php the_excerpt(); ?>
    
    <?php endwhile; else: ?>
    
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    
    <?php endif; ?>

    Is there anything in the code that needs to be improved?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Justin Tadlock

    (@greenshady)

    WordPress God

    You don’t need these two lines:

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>              
    
    <?php query_posts( array($term->taxonomy => $term->name) ); ?>

    query_posts does not work for me with my tanonomy.php template.
    Why?
    Can you help?

    Michael

    (@alchymyth)

    Forum Moderator

    it might help if you can copy the code of our taxonomy.php into a pastebin and post the link here.
    otherwise – what is there to guess from an example showing a nearly empty page?

    Here is a pasteable piece of code to get the taxonomy title for taxonomy.php template.

    <?php
    $term  = $wp_query->query_vars["term"];
    $tax 	= $wp_query->query_vars["taxonomy"];
    $title  = get_term_by( 'slug', get_query_var( $term ), get_query_var( $tax ) );
    ?>

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Automatic post title loop in taxonomy.php’ is closed to new replies.