WordPress.org

Ready to get started?Download WordPress

Forums

Passing queried taxonomy (4 posts)

  1. NWTD
    Member
    Posted 1 year ago #

    I hope I'm posting this in the right forum...

    In my theme, I created a page that lists all terms from a taxonomy, this then links to a page that displays all posts of that taxonomy, each item is then linked to their respective post. The taxonomy template uses <?php single_term_title(''); ?> to display the queried taxonomy term name. How can I pass that queried taxonomy term to the single page template to then be used?

    I tried using the <?php single_term_title(''); ?> but that didn't seem to work. I'm sure it's a simple bit of code, but I can't find it in all my searching.

    Additional information
    This is for a custom post type, "Services" with custom taxonomy, "Locations".

    Here's the part that lists my taxonomies:

    <?php
        $terms = get_terms('locations');
    
            $temp = '';
        foreach($terms as $term):
            $title = $term->name;
            $first_letter_lower = strtolower(substr($title, 0, 1));
            if($temp != $first_letter_lower){
                echo '<div class="locations-'.$first_letter_lower.'">';
                $temp = $first_letter_lower;
            }
    
            $first_letter = strtoupper(substr($title, 0, 1));
            if($temp != $first_letter){
                echo '<h3>'.$first_letter.'</h3><hr />';
                $temp = $first_letter;
            }
    ?>
    <ul>
        <li><a href="<?php echo get_term_link($term);?>"><?php echo $term->name ;?></a></li>
    </ul>
    
    <?php endforeach;?>

    Here's the part for the taxonomy page template:

    <?php if ( have_posts() ) : ?>
        <header class="archive-header">
            <h1 class="archive-title">
                <?php single_term_title('Services in '); ?>
            </h1>
        </header><!-- .archive-header -->
        <ul>
        <?php while ( have_posts() ) : the_post();?>
            <li>
                <a href="<?php the_permalink(); ?>" title="<?php echo esc_attr( sprintf( __( 'Permalink to %s', 'twentytwelve' ), the_title_attribute( 'echo=0' ) ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
            </li>
    
        <?php endwhile;?>
        </ul>
    
    <?php else : ?>
        <?php get_template_part( 'content', 'none' ); ?>
    <?php endif; ?>
  2. dotnordic
    Member
    Posted 1 year ago #

    <?php echo single_tag_title(); ?>

    and/or

    <?php echo single_cat_title(); ?>

  3. NWTD
    Member
    Posted 1 year ago #

    The problem with that solution is that each post is assigned to multiple taxonomy terms. So using <?php echo single_tag_title(); ?> just returns all taxonomy terms of that post.

    I'l like to just reference the one taxonomy term that was clicked on from the get go.

    To get a better idea, take a look at my example: http://chip-smith.com/dev2/areas-we-service/

    If you were to click on "Abbott, CA" (which is a taxonomy term), you are then taken to my page template taxonomy-locations.php and are shown all posts in that taxonomy term "Abbott, CA". Clicking any post takes you to the single-services.php. In that 'single-services.php`, I'd like to reference the queried taxonomy term...so in this case I want to display "Abbott, CA".

    Hope that helps make more sense.

  4. dotnordic
    Member
    Posted 1 year ago #

    A single page doesn't know what you clicked to get there. You'll have to save that information temporarily in a transient or pass that information in the url. You can use a custom query variable.

    http://codex.wordpress.org/Custom_Queries#Custom_Archives

Topic Closed

This topic has been closed to new replies.

About this Topic