Advertisement
munirmahmud6

Taxonomy and Terms

Feb 15th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.33 KB | None | 0 0
  1. <?php
  2. //get the taxonomy term in the template
  3. get_the_term_list( $post->ID, 'taxonomy_name', );
  4.  
  5. //Get term items from a taxonomy
  6. $terms = get_terms( array(
  7.     'taxonomy' => 'taxonomy_name'
  8. ) );
  9. foreach($terms as $term){
  10.     echo "<li><a href="{$term->slug}">{$term->name}</a></li>";
  11. }
  12.  
  13. //get_posts assigned to a specific custom taxonomy term
  14. //https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
  15. $args = [
  16.     'posts_per_page'    => 4,
  17.     'post_type'         => 'recipes',
  18.     'order'             => 'rand',
  19.     'tax_query'         => array(
  20.         array(
  21.             'taxonomy'  => 'course',
  22.             'field'     => 'slug',
  23.             'terms'     => 'desert'
  24.         )
  25.     ),
  26. ];
  27. $query = new WP_Query($args);
  28. while ( $query->have_posts() ) :
  29.     $query->the_post();
  30.     the_title( '<h2>', '</h2>');
  31. endwhile;
  32. wp_reset_postdata();
  33.  
  34.  
  35. //To get taxonomy name dynamically
  36. $term = get_queried_object();
  37. $taxonomy = get_taxonomy( $term->taxonomy );
  38. echo $taxonomy->label . ": " . $term->name;
  39.  
  40. //Show posts and recipes (that is a custom post type)
  41. function print_taxonomy_posts( $query ) {
  42.     if ( !is_admin() && $query->is_main_query() ) {
  43.         if ( is_home() ) {
  44.             $query->set( 'post_type', array( 'post', 'recipes') );
  45.         }
  46.     }
  47. }
  48. add_action( 'pre_get_posts', 'print_taxonomy_posts' );
  49.  
  50. //Get post type name for every post.
  51. //use in the loop.
  52. if( is_home() ) :
  53. <span class="alert"><?php echo get_post_type(); ?></span>
  54. <?php endif; ?>
  55.  
  56.  
  57. //Get posts dynamically based on terms of a taxonomy of posts or custom post types
  58. $terms = get_terms( array(
  59.     'taxonomy' => 'course'
  60. ) );
  61.  
  62. foreach( $terms as $term ){
  63.     echo "<li><a href='{$term->slug}'>{$term->name}</a></li>";
  64. }
  65. function tp_terms_based_posts( $term ) {
  66.     $args = [
  67.         'posts_per_page'    => 4,
  68.         'post_type'         => 'recipes',
  69.         'order'             => 'rand',
  70.         'tax_query'         => array(
  71.             array(
  72.                 'taxonomy'  => 'course',
  73.                 'field'     => 'slug',
  74.                 'terms'     => $term,
  75.             )
  76.         ),
  77.     ];
  78.     $query = new WP_Query($args);
  79.     while ( $query->have_posts() ) :
  80.         $query->the_post();
  81.         echo get_the_title(  );
  82.         echo get_the_post_thumbnail( $post->ID );
  83.     endwhile;
  84.     wp_reset_postdata();    
  85. }
  86.  
  87. foreach ( $term as $term ) {
  88.     tp_terms_based_posts( $term );
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement