WordPress.org

Ready to get started?Download WordPress

Forums

Partial solution to showing single post for a term (2 posts)

  1. richarduk
    Member
    Posted 3 years ago #

    In wordPress, taxonomies are a label applied to a group of 'things' - for example, Book Authors
    Terms are the 'things' themselves - for example, Steven King, Charles Dickens. J.K. Rowling
    Terms are essentially glorified tags. This means that every post can have these tags/terms and these terms can be displayed on a web page.
    A book review site might have several taxonomies - for example, Book Authors, Book Genres, Book Characters.
    When reading a book review, the book's author name might be displayed, the book's genre might be displayed, and a list of the book's characters might be displayed.
    Clicking on the book's author might take us to a list of posts or excerpts, each one of which describes a different book by that author, all with the same tag/term.
    If we want to go to a single post instead here's a partial solution which might be suitable for some purposes.

    Pros: every term in a taxonomy can have its own layout. To labour a point, Term A can go to a page with a series of posts related to that term,Term B can go to a page full of thumbnails/featured imnages, Term C can to a series of excerpts or headings, Term D can go to a page of Custom Fields, Term E can go to a single post, Term F can go to a single image etc.

    Any taxonomy can be over-ridden by taxonomy-(taxonomy_name).php and any term can be be over-ridden by taxonomy-(taxonomy_name)-(term-name).php

    Cons: it doesn't look as if it's possible to include comments for single posts. This is because the code takes place on taxonomy.php, and although comments.php can be included, submitting a comment takes the user to single.php where the comments are displayed. If we go backwards to taxonomy.php the comments are not pulled up.

    Create some posts, some taxonomies and some terms.
    Create several posts with the same name as a term slug. Assign these posts to a category e.g. with an ID of 73 and exclude that category from all other loops on the site.
    If using index.php as a starting point, paste the following inside the loop. This will show all the terms for that post.

    <?php
    //Show all taxonomies and terms for one post. Use inside the loop on multiple post pages, inside or outside the loop on single post pages e.g.     single-default.php
    //Thanks to:  MichaelH   http://wordpress.org/support/topic/display-taxonomies-using-custom-page-template
    foreach ( (array) get_object_taxonomies($post->post_type) as $taxonomy ) {
      $object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
      if ($object_terms) {
        echo '<h4 style="text-align:left;">Terms (for this post) in taxonomy "'.$taxonomy. '"</h4>';
        foreach ($object_terms as $term) {
          echo '<p><a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a></p> ';
        }
      }
    }
    ?>

    At the top of taxonomy.php, perhaps within the main div enclosing your site, paste this (outside the loop). It shows information about the term that was clicked on to arrive at taxonomy.php If you're not bothered about seeing that information then comment out the middle block of the code.

    <?php
    //To display information  about a term AFTER that term has been clicked on and the user has been taken to another
    //  page use this on  taxonomy.php
    //Thanks to:  http://justintadlock.com/archives/2009/06/04/using-custom-taxonomies-to-create-a-book_awards-database
    //See:  http://codex.wordpress.org/Function_Reference/get_term_by
    
    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // THIS BIT IS NECESSARY
    
    echo '<div style="border:1px solid gray;">';
    echo '<h4 style="text-align:left;">This is taxonomy and term information relating to the term you clicked on to arrive here</h4>';
    echo 'Term ID &nbsp;' . $term->term_id . '<br />';
    echo 'Slug &nbsp;' .  $term->slug . '<br />';
    echo 'Name &nbsp;' . $term->name . '<br />';
    echo 'Taxonomy &nbsp;' . $term->taxonomy . '<br />';
    echo 'Term Group &nbsp;' .$term->term_group . '<br />';
    echo 'Count &nbsp;' .$term->count . '<br />';
    echo 'Parent &nbsp;' .$term->parent . '<br />';
    echo 'Description &nbsp;' .$term->description . '<br />';
    echo 'Term Taxonomy ID &nbsp;' .$term->term_taxonomy_id . '<br />';
    echo '</div> <!-- Close taxonomy information div -->';
    
    $taxonomy_name = $term->taxonomy; // THIS BIT IS NECESSARY
    $term_slug = $term->slug; // THIS BIT IS NECESSARY
    ?>

    Now replace your exising loop with something similar to the following, changing taxonomy names as appropriate.

    <?php if ($taxonomy_name == 'author_name')
    	{ ?>
    
    <?php// ******** START TAXONOMY LOOP 1.  SHOW A SINGLE POST ***/?>
    
    <?php   $new_loop = new WP_Query(array('name' => $term_slug, 'posts_per_page' => 1, 'caller_get_posts'=> 1, 'cat' => 73)); ?>
    <?php // Show one post where the post name is the same as the term slug ?>
    
    <?php if( $new_loop->have_posts()): while ($new_loop->have_posts()) : $new_loop->the_post(); ?> 
    
    <h2 class="header category"> <?php the_title();  // No permalink   ?></h2>
    
    <?php  the_post_thumbnail('thumbnail');  //  Show Featured image / thumbnail ?>
    
    <?php $publisher = get_post_meta($post->ID, "publisher", true);   if ($publisher !=='') {echo '<h4>'. $publisher. '</h4>';}  ?>
    <?php // Show custom field ?>
    
    <?php  the_content('<span class="more">[...] </span>'); ?>
    
    <?php endwhile; ?>
    <?/*All of the posts have been fetched but the Loop hasn't finished yet. */?>
    
    <?php else : ?>
    
    <?php // SAY SORRY ?>
    <h2 class="no-posts">Sorry! </h2>
    <?php endif; // End of the loop?>
    
    <?php wp_reset_query();?>
    
    	<?php } 
    
    elseif ($taxonomy_name == 'genre')
    
    	{ ?>
    
    <?php/* START TAXONOMY LOOP 2. SHOW MULTIPLE POSTS WITH THE SAME TERM IN THE SPECIFIED TAXONOMY   */?>
    
    <?php   $new_loop2 = new WP_Query(array('genre' => $term_slug, 'order' => DESC, 'posts_per_page' => 3, 'caller_get_posts'=> 1)); ?>
    <?php // output all the posts with the term name that you clicked on to get here ?>
    
    <?php if( $new_loop2->have_posts()): while ($new_loop2->have_posts()) : $new_loop2->the_post(); ?> 
    
    <h2 class="header category"><a class="permalink" href="<?php the_permalink(); ?>" rel="bookmark" <?php the_title();?></a></h2>
    <?php // Permalink to full post with comments on single.php ?>
    
    <?php  the_post_thumbnail('thumbnail');  //  Show Featured image / thumbnail ?>
    
    <?php $publisher = get_post_meta($post->ID, "publisher", true);   if ($publisher !=='') {echo '<h4>'. $publisher. '</h4>';}  ?>
    <?php // Show custom field ?>
    
    <?php  the_content('<span class="more">[...] </span>'); ?>
    
    <?php endwhile; ?>
    <?/*All of the posts have been fetched but the Loop hasn't finished yet. */?>
    
    <?php else : ?>
    
    <?php // SAY SORRY ?>
    <h2 class="no-posts">Sorry! </h2>
    <?php endif; // End of the loop?>
    
    <?php wp_reset_query();?>
    
    	<?php } 
    
    else { ?>
    
    <?php/*  START TAXONOMY LOOP 3. SHOW MULTIPLE POSTS WITH THE SAME TERM IN ANY TAXONOMY */?> 
    
    <?php query_posts($query_string .'&order=DESC'); ?>
    <?php  if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    <h2 class="header category"><a class="permalink" href="<?php the_permalink(); ?>" rel="bookmark" <?php the_title();?></a></h2>
    
    <?php  the_post_thumbnail('thumbnail');  //  Show Featured image / thumbnail ?>
    
    <?php $publisher = get_post_meta($post->ID, "publisher", true);   if ($publisher !=='') {echo '<h4>'. $publisher. '</h4>';}  ?>
    <?php // Show custom field ?>
    
    <?php  the_content('<span class="more">[...] </span>'); ?>
    
    <?php endwhile; ?>
    <?/*All of the posts have been fetched but the Loop hasn't finished yet. */?>
    
    <?php else : ?>
    
    <?php // SAY SORRY ?>
    <h2 class="no-posts">Sorry! </h2>
    <?php endif; // End of the loop?>
    
    <?php wp_reset_query();?>
    
    	<?php }

    The bit where it says

    <?php // SAY SORRY ?>
    <h2 class="no-posts">Sorry! </h2>

    can be replaced with another loop. That way you can have a default page in your usual layout, with pictures, custom fields etc. all designed from within the WordPress interface. Useful if you make typos or forget to write a post for a specific term.
    Just write a post with the name 'error' (for example') and assign it to the category you're excluding from all other loops. e.g. the category with an ID of 73

    <?php   $new_loop4 = new WP_Query(array('name' => error, 'posts_per_page' => 1, 'caller_get_posts'=> 1, 'cat' => 73)); ?>
    <?php if( $new_loop4->have_posts()): while ($new_loop4->have_posts()) : $new_loop4->the_post(); ?> 
    
    <h2 class="header category"><a class="permalink" href="<?php the_permalink(); ?>" rel="bookmark" <?php the_title();?></a></h2>
    
    <?php  the_post_thumbnail('thumbnail');  //  Show Featured image / thumbnail ?>
    
    <?php $publisher = get_post_meta($post->ID, "publisher", true);   if ($publisher !=='') {echo '<h4>'. $publisher. '</h4>';}  ?>
    <?php // Show custom field ?>
    
    <?php  the_content('<span class="more">[...] </span>'); ?>
    
    <?php endwhile; ?>
    <?/*All of the posts have been fetched but the Loop hasn't finished yet. */?>
    
    <?php else : ?>
    
    <?php // SAY SORRY ?>
    <h2 class="no-posts">Sorry! </h2>
    <?php endif; // End of the loop?>
    
    <?php wp_reset_query();?>

    Hope this all fits in. Thanks for reading! :-)

  2. imscissors
    Member
    Posted 3 years ago #

    Hey Richard,

    Nice... Bookmarked this. This will come in very handy for a project I'm working on right now.

    Thanks,
    Bryan

Topic Closed

This topic has been closed to new replies.

About this Topic