WordPress.org

Ready to get started?Download WordPress

Forums

Single post when clicking on taxonomy term (7 posts)

  1. tshirtfiend
    Member
    Posted 3 years ago #

    I have a menu that lists all of the terms of a (custom) taxonomy. If I click on the term name, I go to a page that is an archive of that term and lists all of the posts labeled with that term. If I click on a post's permalink, I go to the post's page (ie. single.php), and now have the Next and Prev post links.

    I want to be able to click on the taxonomy term in the menu, and just have it display the first post labeled with that term, in a non-archive format. Essentially, it should use single.php instead of archive.php, or something to that effect, and then users would be able to navigate between posts with the same taxonomy via Next/Prev links.

    Does that make sense?

    (I'm testing this with the Twenty Ten theme, just editing functions.php to add the post types and taxonomies.)

  2. MichaelH
    Member
    Posted 3 years ago #

    From my notes:

    Redirect to post if tag page has only post

    <?php
    if(is_tag() && $wp_query->found_posts < 2) {
    	wp_redirect( 'http://someurl' ); exit;
    }
    ?>

    http://wordpress.org/support/topic/317328?replies=2

  3. tshirtfiend
    Member
    Posted 3 years ago #

    I'm not sure if this would work. What I'm trying to do is have WP bypass archive.php, and just display single.php. So I don't want to redirect to just one URL, I want to change which template file WP is looking for when a taxonomy term is clicked.

    Does that make sense?

  4. MichaelH
    Member
    Posted 3 years ago #

    This seems to work.

    Redirect to post permalink if taxonomy archive has only post
    Needs to be added to theme functions.php

    function tax_redirect() {
      global $posts, $wp_query;
    	if ( is_tax() && $wp_query->found_posts == 1 ) {
    	  wp_redirect( get_permalink($posts[0]->ID), 301 );
    	}
    }
    add_action('template_redirect', 'tax_redirect');
  5. tshirtfiend
    Member
    Posted 3 years ago #

    Thanks, but that's not quite what I'm looking for. Here's what I tried doing, so that all taxonomy archives would use the single.php AND only display one post at a time:

    function tax_redirect() {
      global $posts, $wp_query;
    	if ( is_tax() ) {
    	  wp_redirect( get_permalink($posts[0]->ID), 301 );
    	}
    }
    add_action('template_redirect', 'tax_redirect');

    It just showed the first post in that taxonomy, but twice (for some reason). It did use single.php though.

  6. marenm
    Member
    Posted 3 years ago #

    Did anyone ever get a definitive answer on this?
    I've tried both these code snippets in my functions.php file and can't get either of them to work (I'm making a Thematic child theme).
    I've got the TagPages plugin installed so I'd really like for this to work with Pages, rather than posts.

  7. tshirtfiend
    Member
    Posted 3 years ago #

    I never got to the bottom of it. I solved my problem another way, which is a drag, honestly. I understand WP's logic, but it was too difficult to bend it to work with my navigation scheme as I had envisioned it.

Topic Closed

This topic has been closed to new replies.

About this Topic