Reputation: 543
I have a post type called 'dining' and has a taxonomy called 'dining-category'.
What I want to do is, I want to display all the category from post type 'dining' in my footer area.
Upvotes: 19
Views: 110438
Reputation: 1026
The post type is: dining
Taxonomy is: dining-category
For anyone who doesn't know where to get the taxonomy, then you can click on the categories of your custom post type, then you can get the taxonomy from the URL, for example, http://localhost/site/wp-admin/term.php?taxonomy=product_cat&tag_ID=18&post_type=product
. From this URL, you can see my taxonomy is product_cat
use that and replace it with dining-category
from OP.
The following is the code to list all categories from a specific custom post type as per the asked question:
$args = array(
'post_type' =>'dining',
'taxonomy' => 'dining-category',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
);
$categories = get_categories($args);
echo '<ul>';
foreach ($categories as $category) {
$url = get_term_link($category);?>
<li><a href="<?php echo $url;?>"><?php echo $category->name; ?></a></li>
<?php
}
echo '</ul>';
Upvotes: 0
Reputation: 868
<?php
$args = array(
'type' => 'dining',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'taxonomy' => 'dining-category',
'pad_counts' => false );
$categories = get_categories($args);
echo '<ul>';
foreach ($categories as $category) {
$url = get_term_link($category);?>
<li><a href="<?php echo $url;?>"><?php echo $category->name; ?></a></li>
<?php
}
echo '</ul>';
?>
If category not assigned any post it will not show. therefore assign any post. This code running perfectly.
Upvotes: 14
Reputation: 342
<?php
$wcatTerms = get_terms(
'category', array('hide_empty' => 0, 'number' => 3, 'order' =>'asc', 'parent' =>0));
foreach($wcatTerms as $wcatTerm) :
?>
<small><a href="<?php echo get_term_link( $wcatTerm->slug, $wcatTerm->taxonomy ); ?>"><?php echo $wcatTerm->name; ?></a></small>
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $wcatTerm->slug,
)
),
'posts_per_page' => 1
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$imgurl = get_the_post_thumbnail_url( get_the_ID(), 'full' );
$title=get_the_title($post->ID);
?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; wp_reset_postdata(); ?>
<?php endforeach; ?>
Upvotes: 2
Reputation: 2514
In WordPress 4.6 get_terms
is deprecated. So there is an alternate of this (get_categories
) Read this
And here is Example code:
<?php
$args = array(
'taxonomy' => 'dining-category',
'orderby' => 'name',
'order' => 'ASC'
);
$cats = get_categories($args);
foreach($cats as $cat) {
?>
<a href="<?php echo get_category_link( $cat->term_id ) ?>">
<?php echo $cat->name; ?>
</a>
<?php
}
?>
Hope this will help you.
Upvotes: 44
Reputation: 188
use get_terms()
function to fetch custom taxonomy by its slug, in your case slug is dining-category.
read function refrence from wordpress codex website and try this.
Upvotes: -2