Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //get the taxonomy term in the template
- get_the_term_list( $post->ID, 'taxonomy_name', );
- //Get term items from a taxonomy
- $terms = get_terms( array(
- 'taxonomy' => 'taxonomy_name'
- ) );
- foreach($terms as $term){
- echo "<li><a href="{$term->slug}">{$term->name}</a></li>";
- }
- //get_posts assigned to a specific custom taxonomy term
- //https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
- $args = [
- 'posts_per_page' => 4,
- 'post_type' => 'recipes',
- 'order' => 'rand',
- 'tax_query' => array(
- array(
- 'taxonomy' => 'course',
- 'field' => 'slug',
- 'terms' => 'desert'
- )
- ),
- ];
- $query = new WP_Query($args);
- while ( $query->have_posts() ) :
- $query->the_post();
- the_title( '<h2>', '</h2>');
- endwhile;
- wp_reset_postdata();
- //To get taxonomy name dynamically
- $term = get_queried_object();
- $taxonomy = get_taxonomy( $term->taxonomy );
- echo $taxonomy->label . ": " . $term->name;
- //Show posts and recipes (that is a custom post type)
- function print_taxonomy_posts( $query ) {
- if ( !is_admin() && $query->is_main_query() ) {
- if ( is_home() ) {
- $query->set( 'post_type', array( 'post', 'recipes') );
- }
- }
- }
- add_action( 'pre_get_posts', 'print_taxonomy_posts' );
- //Get post type name for every post.
- //use in the loop.
- if( is_home() ) :
- <span class="alert"><?php echo get_post_type(); ?></span>
- <?php endif; ?>
- //Get posts dynamically based on terms of a taxonomy of posts or custom post types
- $terms = get_terms( array(
- 'taxonomy' => 'course'
- ) );
- foreach( $terms as $term ){
- echo "<li><a href='{$term->slug}'>{$term->name}</a></li>";
- }
- function tp_terms_based_posts( $term ) {
- $args = [
- 'posts_per_page' => 4,
- 'post_type' => 'recipes',
- 'order' => 'rand',
- 'tax_query' => array(
- array(
- 'taxonomy' => 'course',
- 'field' => 'slug',
- 'terms' => $term,
- )
- ),
- ];
- $query = new WP_Query($args);
- while ( $query->have_posts() ) :
- $query->the_post();
- echo get_the_title( );
- echo get_the_post_thumbnail( $post->ID );
- endwhile;
- wp_reset_postdata();
- }
- foreach ( $term as $term ) {
- tp_terms_based_posts( $term );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement