I am trying to show all records for a particular custom post type and I want a list of all taxonomies in a dropdown.
Here is the query for the CPT:
<?php
$articles = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'stories',
));
?>
And getting the terms
<?php
$terms = get_terms( array(
'taxonomy' => 'topic',
'hide_empty' => false,
) );
?>
Then the loop
<?php if ($articles->have_posts() ):
while ($articles->have_posts() ): $articles->the_post();
?>
// this is where I want to echo all taxonomy names
<div class="masonry__item col-lg-3 col-md-6" data-masonry-filter="<?php //echo tax names here ?>">
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
I am trying to show all records for a particular custom post type and I want a list of all taxonomies in a dropdown.
Here is the query for the CPT:
<?php
$articles = new WP_Query(array(
'posts_per_page' => -1,
'post_type' => 'stories',
));
?>
And getting the terms
<?php
$terms = get_terms( array(
'taxonomy' => 'topic',
'hide_empty' => false,
) );
?>
Then the loop
<?php if ($articles->have_posts() ):
while ($articles->have_posts() ): $articles->the_post();
?>
// this is where I want to echo all taxonomy names
<div class="masonry__item col-lg-3 col-md-6" data-masonry-filter="<?php //echo tax names here ?>">
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Share
Improve this question
asked Nov 21, 2019 at 9:42
user10980228user10980228
1691 silver badge14 bronze badges
5
|
1 Answer
Reset to default 1I'm answering the <?php //echo tax names here ?>
:
In the loop, you can get the post terms using
wp_get_post_terms()
:while ($articles->have_posts() ): $articles->the_post(); $terms = wp_get_post_terms( get_the_ID(), 'topic', [ 'fields' => 'names' ] );
Then you can do something like
echo implode( ' ', $terms );
inside the<div>
tag:<div class="masonry__item col-lg-3 col-md-6" data-masonry-filter="<?php echo implode( ' ', $terms ); ?>">
But are you very sure you want to use the term name and not slug?
If you want the term slug, then you'd use 'fields' => 'id=>slug'
and not 'fields' => 'names'
.
Category 1, Category 2
(separated by comma) or is it by slug like so:category-1 category-2
(separated by space) – Sally CJ Commented Nov 21, 2019 at 11:20