Display specific taxonomy

admin2025-01-07  5

I have a custom post type LOCATIONS I have a custom taxonomy LANDMARKS Under LANDMARKS, I have a list of building landmarks On some pages/posts, I want to be able to insert specific landmark from the taxonomy list instead of displaying the entire list.

I thought I can use custom fields but I would prefer it to work like taxonomy where upon user click, it will show all posts related to that landmark.

After a search, it seems that it is not possible to display a single taxonomy term? This is the snippet I'm using to display taxonomy list.

function list_terms_custom_taxonomy( $atts) {
extract( shortcode_atts( array(
    'custom_taxonomy' => '',
), $atts ) );

ob_start();
global $post;
$string1 = '<ul class="tax_listing">';
$string1 .= get_the_term_list( $post->ID , $custom_taxonomy, '<li>', '</li>' . ', ' . '<li>', '</li>' );
$string1 .= ob_get_clean();
$string1 .= '</ul>';
return $string1;
}

add_shortcode( 'wp', 'list_terms_custom_taxonomy' );
add_filter('widget_text', 'do_shortcode');

Additional info: I'm using TYPES plugin.

I have a custom post type LOCATIONS I have a custom taxonomy LANDMARKS Under LANDMARKS, I have a list of building landmarks On some pages/posts, I want to be able to insert specific landmark from the taxonomy list instead of displaying the entire list.

I thought I can use custom fields but I would prefer it to work like taxonomy where upon user click, it will show all posts related to that landmark.

After a search, it seems that it is not possible to display a single taxonomy term? This is the snippet I'm using to display taxonomy list.

function list_terms_custom_taxonomy( $atts) {
extract( shortcode_atts( array(
    'custom_taxonomy' => '',
), $atts ) );

ob_start();
global $post;
$string1 = '<ul class="tax_listing">';
$string1 .= get_the_term_list( $post->ID , $custom_taxonomy, '<li>', '</li>' . ', ' . '<li>', '</li>' );
$string1 .= ob_get_clean();
$string1 .= '</ul>';
return $string1;
}

add_shortcode( 'wp', 'list_terms_custom_taxonomy' );
add_filter('widget_text', 'do_shortcode');

Additional info: I'm using TYPES plugin.

Share Improve this question edited Jan 11, 2015 at 3:26 Pieter Goosen 55.4k23 gold badges115 silver badges209 bronze badges asked Jan 11, 2015 at 1:56 JaniceJanice 93 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

you need to use this code to get all the terms :

 <?php $custom_terms= get_terms('locations');                          
       foreach($custom_terms as $term):
 ?>
       <a href="<?php echo get_term_link( $term->name, 'locations' ); ?>" class="browse_more"><?phpecho $term->name; ?>
       </a>  
 <?php   endforeach;               
 ?> 

This will generate all the terms and when you click term you will be taken to the page of that specific custom terms and there you can use the loop to get all the posts for that specific category. You will need to create the file taxonomy.php
Put the following code there

<?php
    $term = get_queried_object();
    $args =array(
                 'post_type' => 'packages',
                 'posts_per_page' =>6, 
                 'tax_query' =>
                         array(
                               array(
                                     'taxonomy' => 'locations',
                                     'field'    => 'name',
                                     'terms'    => $term->name,
                                     ),
                               ),
                  );
  $loop = new WP_Query( $args );                                             
  if($loop->have_posts()):while ( $loop->have_posts() ): $loop->the_post();
the_title();
the_content();
endwhile;
endif;
?>

I hope this solves your problemO.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260779a689.html

最新回复(0)