Tag Archives: Taxonomy

Get a list of registered taxonomy

Get a list of registered taxonomy without link

 <?php
              	$terms = get_terms( 'features' );
				 if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
					 echo '<ul>';
					 foreach ( $terms as $term ) {
					   echo '<li>' . $term->name . '</li>';
						
					 }
					 echo '</ul>';
				 }
			   ?>

Get a list of registered taxonomy with link

<?php
					$args = array( 'hide_empty' => 0 );
					$terms = get_terms( 'features', $args );
					if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
						$count = count( $terms );
						$i = 0;
						$term_list = '<p class="features-archive">';
						foreach ( $terms as $term ) {
							$i++;
							$term_list .= '<a href="' . get_term_link( $term ) . '" title="' . sprintf( __( 'View all post filed under %s', 'my_localization_domain' ), $term->name ) . '">' . $term->name . '</a>';
							if ( $count != $i ) {
								$term_list .= ' , ';
							}
							else {
								$term_list .= '</p>';
							}
					}
					echo $term_list;
				}
				?>

Get a list of registered taxonomy By ID in single post page

<?php 
				  foreach((get_the_terms($post->ID, 'html-tag')) as $term)
						 {
							$term_link = get_term_link( $term );
   							// If there was an error, continue to the next term.
    							if ( is_wp_error( $term_link ) ) {
        							continue;
    							}
							echo '<a href="' . esc_url( $term_link ) . '">' . $term->name . '</a>, ','';
						}
                  ?>

Taxonomy and Tags for Custom post type

Tags and Taxonomy both are using register_taxonomy() function.

if you want to display Taxonomy so ‘hierarchical’      => true,

if you want to display Tags so ‘hierarchical’      => false,


/***** Adding custom post type, Texonomys, Tags - Our Team *****/
function ourteams_custom_init() {
$labels = array(
'name' => 'Our Team',
'singular_name' => 'ourteams',
'add_new' => 'Add New',
'add_new_item' => 'Add New Our Team',
'edit_item' => 'Edit Our Team',
'new_item' => 'New Our Team',
'all_items' => 'All Our Team',
'view_item' => 'View Our Team',
'search_items' => 'Search Our Team',
'not_found' => 'No plans found',
'not_found_in_trash' => 'No plans found in Trash',
'parent_item_colon' => '',
'menu_name' => 'Our Team'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'ourteams' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title' ,'thumbnail' , 'editor' ),

'taxonomies' => array( 'members')
);

register_post_type( 'ourteams', $args );

// team Tags
$members_labels = array(
'name'              => _x( 'Members', 'taxonomy general name' ),
'singular_name'     => _x( 'Members', 'taxonomy singular name' ),
'search_items'      => __( 'Search Members' ),
'all_items'         => __( 'All Members' ),
'parent_item'       => __( 'Parent Members' ),
'parent_item_colon' => __( 'Parent Members:' ),
'edit_item'         => __( 'Edit Members' ),
'update_item'       => __( 'Update Members' ),
'add_new_item'      => __( 'Add New Members' ),
'new_item_name'     => __( 'New Members' ),
'menu_name'         => __( 'Members' ),
);

$members_labels_args = array(
'hierarchical'      => true,
'labels'            => $members_labels,
'show_ui'           => true,
'show_admin_column' => true,
'query_var'         => true,
'show_in_nav_menus' => true,
'show_tagcloud'     => true,
'rewrite'           => array( 'slug' => 'members' ),
);
register_taxonomy( 'members', array( 'ourteams' ), $members_labels_args );
}
add_action( 'init', 'ourteams_custom_init' );

Display Post by Category

/* Show posts associated with certain categories.
cat (int) – use category id.
category_name (string) – use category slug (NOT name).
category__and (array) – use category id.
category__in (array) – use category id.
category__not_in (array) – use category id. */

// If You are Using Custom post type so you have to use ‘tax_query’


$args_ourteams=array(
'post_type' => 'ourteams',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'members',
//'field' => 'term_id',
//'terms' => 10,
'field' => 'slug',
'terms' => 'attorneys'
)));

$loop_ourteams = new WP_Query($args_ourteams);

while ($loop_ourteams->have_posts()) : $loop_ourteams->the_post();

the_title();

endwhile;

%d bloggers like this: