Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

related posts by custom taxonomy

By: a guest on Jun 18th, 2012  |  syntax: None  |  size: 2.49 KB  |  views: 59  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. First of all I add my taxonomy "Clienti" in the file functions.php:
  2. `/**
  3.  * Add custom taxonomies
  4.  *
  5.  * Additional custom taxonomies can be defined here
  6.  * http://codex.wordpress.org/Function_Reference/register_taxonomy
  7.  */
  8.  
  9. function add_custom_taxonomies() {
  10.         // Add new "Clienti" taxonomy to Posts
  11.         register_taxonomy('cliente', 'post', array(
  12.                 // Hierarchical taxonomy (like categories)
  13.                 'hierarchical' => true,
  14.                 // This array of options controls the labels displayed in the WordPress Admin UI
  15.                 'labels' => array(
  16.                         'name' => _x( 'Clienti', 'taxonomy general name' ),
  17.                         'singular_name' => _x( 'Cliente', 'taxonomy singular name' ),
  18.                         'search_items' =>  __( 'Cerca Clienti' ),
  19.                         'all_items' => __( 'Tutti i clienti' ),
  20.                         'parent_item' => __( 'Cliente genitore' ),
  21.                         'parent_item_colon' => __( 'cliente genitore:' ),
  22.                         'edit_item' => __( 'Edit Cliente' ),
  23.                         'update_item' => __( 'Update Cliente' ),
  24.                         'add_new_item' => __( 'Aggiungi nuovo Cliente' ),
  25.                         'new_item_name' => __( 'Nuovo nome cliente' ),
  26.                         'menu_name' => __( 'Clienti' ),
  27.                 ),
  28.                 // Control the slugs used for this taxonomy
  29.                 'rewrite' => array(
  30.                         'slug' => 'clienti', // This controls the base slug that will display before each term
  31.                         'with_front' => false, // Don't display the category base before "/locations/"
  32.                         'hierarchical' => true // This will allow URL's like "/locations/boston/cambridge/"
  33.                 ),
  34.         ));
  35. }
  36. add_action( 'init', 'add_custom_taxonomies', 0 );`
  37.  
  38. Then I added this code in my loop.I also found the way to show my posts randomly.
  39.  
  40. `<div class="sidebarpost">            
  41. <?php
  42. global $post;
  43. remove_all_filters('posts_orderby');
  44.  
  45. $terms = get_the_terms( $post->ID , 'cliente', 'string');
  46. $do_not_duplicate[] = $post->ID;
  47.  
  48. if(!empty($terms)){
  49.     foreach ($terms as $term) {
  50.         query_posts( array(
  51.         'cliente' => $term->slug,
  52.         'showposts' => 5,
  53.         'caller_get_posts' => 1,
  54.                 'orderby' => 'rand',
  55.         'post__not_in' => $do_not_duplicate ) );
  56.         if(have_posts()){
  57.             while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>
  58.            
  59.             <li class="articlecontextual">
  60.             <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
  61.             <?php the_ID(); ?>"
  62.                 <h2><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h2>
  63.             </li>
  64.             <?php endwhile; wp_reset_query();
  65.         }
  66.     }
  67. }
  68. ?>            
  69. </div> `