WordPress.org

Forums

Automatic post title loop in taxonomy.php (6 posts)

  1. design_dolphin
    Member
    Posted 5 years ago #

    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?

  2. Justin Tadlock

    Posted 5 years ago #

    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) ); ?>
  3. liove
    Member
    Posted 5 years ago #

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

  4. liove
    Member
    Posted 5 years ago #

  5. alchymyth
    Forum Moderator
    Posted 5 years ago #

    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?

  6. avenueverve
    Member
    Posted 5 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.